zoukankan      html  css  js  c++  java
  • Backbone学习记录(6)

    路由

    backbone将路由规则和一个方法名绑定到一起,来控制单页的hash,以及单页的前进后退。

    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 error hash!");
      }
    });
    var user_router=new UserRouter();
    
    Backbone.history.start();//开启前history监控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/backbone_test.html#other
    //other is a error hash!

    路由规则

    search/:query中 : 表示查询,,当输入#search/page 这里的page就作为参数传给与规则search/:query绑定的函数中。

    *表示匹配0或多个字符 ,如果上面的例子中没有这个"": "main"规则,url后面输入#,控制台就会显示 null "is a error hash!" ,因为 * 匹配了0个字符。
    #之后的所有字符,都可以作为一个整体字符串的参数传给和该规则绑定的方法中。*也可以和:混用。比如对于规则"*search/:query/error": "error",url后面输入“#search/page/url”,“search/page/url”就作为参数传给了error方法。

    Backbone.history.start()或Backbone.history.start({pushState: true})用于开启history监控,这样就能应用路由规则了。
  • 相关阅读:
    -1%256的值是多少?
    Glut,程序的基本架构
    剑指offer:数值的整数次方
    剑指offer:二进制中1的个数
    剑指offer:斐波那契数列的应用
    剑指offer:斐波那契数列
    剑指offer:旋转数组中的最小数字
    弱智的grub消除法
    POJ 3126 Prime Path
    HDU 1426 Sudoku Killer
  • 原文地址:https://www.cnblogs.com/qianlegeqian/p/3948327.html
Copyright © 2011-2022 走看看