Backbone learning records (6)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.