在Backbone中,有没有办法触发路由事件处理程序,没有更改URL?
我的意思是我想触发路由处理程序,但我不想更改URL。 因此,我不想使用
router.navigate(route, {trigger: true});
因为这会导致URL发生变化。
答案 0 :(得分:2)
路由器本身连接到一个功能。简单的答案是直接调用函数,简单地绕过路径处理。
(function( $, Backbone ) {
var exports = window.app = window.app || {},
Router = Backbone.Router.extend({
// Here you declare what the routes are in your router
// and what functionality they should trigger.
routes: {
"help" : "help",
"search/:query" : "search",
"search/:query/p:page": "search"
},
// Declare route functions.
help : function() {},
search: function( query, page ) {}
});
// Export the router.
exports.router = new Router();
// Just a dummy object for calling the router.
var cookieMonster = {
init: function() {
// Do something on init.
// End with calling the route help function.
exports.router.help();
}
};
}(jQuery, Backbone));
在这种情况下, cookieMonster.init()
会以调用路由器中的help
函数结束。
一个提示是看看Backbone Marionette,你有一个控制器,它具有从路线中分离出来的功能逻辑,这是使Marionette很棒的很多事情之一。
答案 1 :(得分:1)
为了它的价值,Marionette路由在这里进行了广泛的解释:http://samples.leanpub.com/marionette-gentle-introduction-sample.pdf
讨论的策略是将URL管理与应用程序反应(例如,切换子应用程序)分开。这意味着您可以自由地让您的应用触发处理程序(使用Marionette事件),而无需修改URl片段。
答案 2 :(得分:0)
您是否尝试过Backbone.history.loadUrl(route);
?