Pol*_*ino 9 routing backbone.js backbone-routing
这是我的application-router.js
文件,我Backbone.Router
只用几条路线创建对象:
var App = App || {};
App.Router = Backbone.Router.extend({
routes : {
'' : 'showDashboard', // Not shown
'*other': 'showModalError'
},
defaultRoute : function(other) { $('#modal404').modal(); }
});
Run Code Online (Sandbox Code Playgroud)
在主javascript文件中,application.js
我想以编程方式添加路由.我已尝试使用route()函数,但它不起作用,不添加路由.然而它将一个对象传递给"构造函数",但是它会覆盖已定义的路径:
// 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');
Run Code Online (Sandbox Code Playgroud)
实际上console.log(App.Router)
表明:
routes Object { /test/me="testRoute"}
Run Code Online (Sandbox Code Playgroud)
我想我错过了一些我无法弄清楚的东西,我开始学习这一小块强大的javascript.
mu *_*ort 14
你的router.route
通话工作正常,这些通话不是你的问题.当您呼叫route
添加新路由时,新路由将在路由列表的末尾.特别是,您的route
呼叫添加的路由会跟随'*other'
并'*other'
匹配任何内容,因此您的新路由将被有效忽略.
尝试'*other'
从routes
两次route()
调用后删除您的路由并添加它:
routes : {
'' : 'showDashboard' // Not shown
},
router.route(ExposeTranslation.get('customers.new.route'), 'newCustomer');
router.route('/test/me/again', 'testAgainRoute');
router.route('*other', 'showModalError');
Run Code Online (Sandbox Code Playgroud)
路线不存储在App.Router
对象中,它们存储在内部Backbone.history
:
route: function(route, name, callback) {
// ...
Backbone.history.route(route, _.bind(function(fragment) {
//...
}, this));
return this;
},
Run Code Online (Sandbox Code Playgroud)
这就是为什么你console.log(App.Router)
没有说任何有用的原因.
归档时间: |
|
查看次数: |
5160 次 |
最近记录: |