具有两个属性的基本路由

时间:2014-01-14 23:37:36

标签: url-routing canjs

我使用属性,搜索和注册车辆进行此路由测试

我的代码

$(function() {

  Router = can.Control({

      "vehicles" : function(){
        console.log("the hash is #!vehicles")
      },
      "vehicles/:registration" : function(data){
        console.log("the hash is #!vehicles/:registration "+data)
      },
      "vehicles/:search" : function(data){
        console.log("the hash is #!vehicles/:search "+data)
      }

    });


    can.route("vehicles");
    can.route("vehicles/:registration");
    can.route("vehicles/:search");

    can.route.ready();

    new Router(document);

});

和测试

测试一个

  
    

can.route.attr(“搜索”,“121-TYU-898”)

  
a {_data: Object, _cid: ".map1", _computedBindings: Object, __bindEvents: Object, _bindings: 2…}
__bindEvents: Object
_bindings: 2
_cid: ".map1"
_computedBindings: Object
_data: Object
route: "vehicles/:search"
search: "121-TYU-898"
__proto__: t.Construct

没关系,但在控制台中没有消息(“哈希是#!vehicles /:search”+ data)

测试两个

  
    

can.route.attr(“registration”,“333-TYU-898”)

  
a {_data: Object, _cid: ".map1", _computedBindings: Object, __bindEvents: Object, _bindings: 2…}
__bindEvents: Object
_bindings: 2
_cid: ".map1"
_computedBindings: Object
_data: Object
registration: "121-TYU-898"
route: "vehicles/:registration"
__proto__: t.Construct

没关系,但在控制台中没有消息(“哈希是#!vehicles /:registration”+ data)

测试三

  
    

can.route.attr(“搜索”,“444-TYU-555”)

  
a {_data: Object, _cid: ".map1", _computedBindings: Object, __bindEvents: Object, _bindings: 2…}
__bindEvents: Object
_bindings: 2
_cid: ".map1"
_computedBindings: Object
_data: Object
registration: "121-TYU-898"
route: "vehicles/:registration"
search: "333-TYU-808"
__proto__: t.Construct

我不明白,对我来说,我期待这个答案

a {_data: Object, _cid: ".map1", _computedBindings: Object, __bindEvents: Object, _bindings: 2…}
__bindEvents: Object
_bindings: 2
_cid: ".map1"
_computedBindings: Object
_data: Object
route: "vehicles/:search"
search: "444-TYU-555"
__proto__: t.Construct

你能帮忙理解吗,谢谢

非常感谢Daff,我可以做到这一点吗?

在属性页面是特定值时监听?

例如

$(function() {

  Router = can.Control({

      "route": function(){
        console.log("the hash is empty")
      },
      '{can.route} page=search': function(data){
              console.log("the hash is "+data.page+" with id "+data.id )

      },{can.route} page=registration': function(data){
              console.log("the hash is "+data.page)
      }

    });

    can.route(':page/:id');
    can.route.ready();

    new Router(window);

});

  
    

can.route.attr({page:“search”,id:“123-jlkj-1231”})

  

并在控制台中显示

  
    

散列是使用id 123-jlkj-1231

进行搜索   

我测试了,但这不起作用:)

你知道我想做什么吗?

1 个答案:

答案 0 :(得分:1)

想象一下CanJS路由占位符就像JavaScript中的变量名一样。像

这样的东西
function first(param1) {
}

function first(param2) {
}

与匹配相同路线的vehicles/:registrationvehicles/:type相同。您可能想要做的是:

var Router = can.Control({
  "vehicles route" : function(){
    console.log("the hash is #!vehicles")
  },
  "vehicles/:type route" : function(data){
    console.log(data.type);
  }
});

new Router(document);
can.route.ready();

如此小提琴所示:http://jsfiddle.net/4M58j/

您还可以匹配"vehicles/search""vehicles/registration route"等路线(请注意缺少的:用作变量掌状。