编程加入航线Backbone.Router? [英] Programmatically adding routes to Backbone.Router?

查看:204
本文介绍了编程加入航线Backbone.Router?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的应用router.js 文件,其中我创建 Backbone.Router 对象,仅仅只有几条路线:

Here is my application-router.js file where i'm creating Backbone.Router object with just only few routes:

var App = App || {};

App.Router =  Backbone.Router.extend({
    routes : {
        ''      : 'showDashboard', // Not shown
        '*other': 'showModalError'
    },
    defaultRoute : function(other) { $('#modal404').modal(); }
});

在主JavaScript文件的application.js 我想以编程方式添加路由。我试着路线()函数,它不工作,不添加路由。然而,它的工作原理传递对象的构造,但将覆盖已定义的路线:

In main javascript file application.js i'd like to programmatically add routes. I've tried with route() function and it doesn't work, routes are not added. It works however passing an object to the "constructor", but that will override already defined routes:

// This works and overrides all defined routes in App.Router
var router = new App.Router({ routes : { '/test/me' : 'testRoute' } });

// This is not working
router.route(ExposeTranslation.get('customers.new.route'), 'newCustomer');
router.route('/test/me/again', 'testAgainRoute');

在事实上的console.log(App.Router)显示:

routes Object { /test/me="testRoute"}

我想我失去了一些东西我想不通,我开始学习这种小片强大的JavaScript的。

I suppose i'm missing something i can't figure out, i'm beginning learning this little piece of powerful javascript.

推荐答案

router.route 呼叫工作,这些电话是不是你的问题。当你调用 路线 添加一个新的途径,新干线去在路由列表的末尾。特别是,由你的路由添加路由来电之后'*等'*等让你的新航线将被有效地忽略将匹配任何东西。

Your router.route calls are working, those calls aren't your problem. When you call route to add a new route, the new route goes at the end of the routing list. In particular, the routes that are added by your route calls go after '*other' and '*other' will match anything so your new routes will be effectively ignored.

尝试删除您的'*等从路线路线和你的两个路线()调用:

Try removing your '*other' route from routes and adding it after your two route() calls:

routes : {
    ''      : 'showDashboard' // Not shown
},

router.route(ExposeTranslation.get('customers.new.route'), 'newCustomer');
router.route('/test/me/again', 'testAgainRoute');
router.route('*other', 'showModalError');

的路由不存储在 App.Router 对象的they're里面存放的 Backbone.history

route: function(route, name, callback) {
  // ...
  Backbone.history.route(route, _.bind(function(fragment) {
    //...
  }, this));
  return this;
},

这就是为什么你的的console.log(App.Router)不说什么帮助。

That's why your console.log(App.Router) doesn't say anything helpful.

这篇关于编程加入航线Backbone.Router?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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