Backbone.js Essentials by Jeremy Walker
Author:Jeremy Walker [Walker, Jeremy]
Language: eng
Format: epub
Tags: Web, Computers, Open Source, Programming Languages, JavaScript, Programming, Web Programming
ISBN: 9781784395322
Google: XZ-9CQAAQBAJ
Publisher: Packt Publishing Ltd
Published: 2015-05-29T21:08:53+00:00
Luckily, there are two alternatives that solve this problem: routing strings and regular expression routes. First, let's look at how regular expression routes can solve this problem:
SiteRouter = new Backbone.Router({ initialize: function(options) { // This regex will match "book/" followed by a number this.route(/^book\/(\d+)$/, 'bookRoute'); }, bookRoute: function(bookId) { // book route logic would go here } });
As you can see in the preceding example, we were able to create a single route with a regular expression that matches all our possible book routes. Because this expression contains a group (the part of the regular expression that is surrounded by parentheses), Backbone will pass the matching part of the route to the routing function as an argument, allowing us to know the book's ID from within our routing function. If we were to include multiple groups in our regular expression, Backbone would have provided each matching part of the route as a separate argument to our routing function.
Regular expressions are powerful, which makes them a good choice for defining routes. However, they have one major downside: they are hard for humans to read, especially when you revisit them months after writing them. As the old programming adage goes: you had a problem and tried to solve it using regular expressions. Now you have two problems. For this reason, Backbone provides a third option for defining routes: routing strings.
Routing strings are similar to regular expressions; in that, they let you define dynamic routes but are much more limited. You can only define groups (also known as parameters), wildcard groups (also known as splats), and optional parts. By trading away some of the power of regular expressions, routing strings gain a great deal of readability. Instead of using (\d+) to match a part of the route for a book's ID, a routing string will use the more readable bookId.
Just as with a regular expression route, Backbone will provide the route's parameters as arguments to the routing function, as shown here:
SiteRouter = new Backbone.Router({ initialize: function(options) { this.route('book/:bookId', 'bookRoute'); }, bookRoute: function(bookId) { // book route logic would go here } });
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12570)
Hello! Python by Anthony Briggs(9915)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9778)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7072)
Microservices with Go by Alexander Shuiskov(6834)
Practical Design Patterns for Java Developers by Miroslav Wengner(6755)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6697)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6099)
The Art of Crafting User Stories by The Art of Crafting User Stories(5628)
NetSuite for Consultants - Second Edition by Peter Ries(5562)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5369)
Kotlin in Action by Dmitry Jemerov(5062)
