为什么骨干路由需要默认路由 [英] why route of backbone needs the default route

查看:208
本文介绍了为什么骨干路由需要默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个测试用例Backbone.js的@:
  http://jsfiddle.net/VWBvs/5/

I create a test case with backbone.js @: http://jsfiddle.net/VWBvs/5/

路线定义为

var AppRouter = Backbone.Router.extend({
        routes: {
            "/posts/:id" : "getPost",
            "/download/*path": "downloadFile",  
            "*actions" : "defaultRoute"
        },
        getPost: function(id) {
            alert(id);
        },
        defaultRoute : function(actions){
            alert(actions);
        },
        downloadFile: function( path ){ 
            alert(path); // user/images/hey.gif 
        },
        loadView: function( route, action ){ 
            alert(route + "_" + action); // dashboard_graph 
        }
    });

    var app_router = new AppRouter;

    Backbone.history.start();​

当我改变功能

  defaultRoute : function(actions){
            alert(actions);
        },

defaultRoute : function(actions){
            var action = actions
        },

所有其他途径将无法正常工作,这意味着没有弹出对话框。

all other routes won't work which means no dialog pops up.

不过,重新改变了code时,一切正常。

But when rechange the code ,all is ok.

这很古怪,让我困惑。
SOS真诚......

It's really weird and make me confused. SOS sincerely ......

推荐答案

当你有code 默认路由是有史以来发射的唯一途径。如果你想其他两个路线解雇你必须删除斜线开头。

As you have the code defaultRoute is the only route that ever fires. If you want the other two routes to fire you have to remove the leading slashes.

routes: {
  "posts/:id" : "getPost",
  "download/*path": "downloadFile",  
  "*actions" : "defaultRoute"
}

这篇关于为什么骨干路由需要默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆