Routing
Backbone binds a routing rule with a method name to control the hash of a single page and the forward and backward of a single page.
VaR userrouter = backbone. router. extend ({routes: {"": "Main", "help": "Help", // # Help "Search/: Query": "Search ", // # search/page "Search/: Query/P: page": "Search", // # search/page/P1 "* error": "error ",}, main: function () {console. log ("Main") ;}, help: function () {console. log ("help") ;}, search: function (query, page) {console. log (query, page) ;}, error: function (error) {console. log (error, "is a err Or hash! ") ;}}); Var user_router = new userrouter (); backbone. history. start (); // history monitoring hashchange // http: // localhost/backbone_test/backbone_test.html // => main/HTTP: // localhost/backbone_test/backbone_test.html # Help // => help // http: // localhost/backbone_test/backbone_test.html # search/Page // page null // http: // localhost/backbone_test/backbone_test.html # search/page/P1 // page 1 // http: // localhost/backbone_test/BAC Kbone_test.html # other // other is a error hash!
Routing rules
Search/: Query: indicates a query. When you enter # search/page, the page is passed as a parameter to the function bound to the search/: Query rule.
* Matches 0 or multiple characters. If the ":" Main "rule is not found in the preceding example, enter # After the URL, and the console displays null" is a error hash! ", Because * matches 0 characters.
# All subsequent characters can be passed as a whole string parameter to the method bound to this rule. * It can also be used in combination. For example, for the rule "* search/: Query/error": "error", enter "# search/page/url" after the URL ", "Search/page/url" is passed as a parameter to the error method.
Backbone. History. Start () or backbone. History. Start ({pushstate: true}) is used to enable history monitoring so that routing rules can be applied.
Backbone learning records (6)