admin管理员组文章数量:1362697
Does in Backbone.Router
exists the events both before and after performing routing?
My app works with jQuery.mobile
and call to $.mobile.changePage was needed before route was performed and in controller have access to currently showing page by $.activePage. When action of controller is done, I should trigger the create
event on document to get 'enhanced' by $.mobile newly created elements.
I did this by replacing loadUrl
Backbone.history.loadUrl = ( function( old ){
return function() {
Router.trigger("all:before");
old.apply( Backbone.history, arguments );
Router.trigger("all:after" );
}
})( Backbone.history.loadUrl );
//Router.initialize
initialize: function() {
this.bind( "all:before", function( ) {
$.mobile.changePage( window.location.hash.split( "/" )[0] || "home", { changeHash:false} );
});
this.bind( "all:after", function() {
$.mobile.activePage.trigger('create');
});
}
Is there maybe some built-in events like this?
Does in Backbone.Router
exists the events both before and after performing routing?
My app works with jQuery.mobile
and call to $.mobile.changePage was needed before route was performed and in controller have access to currently showing page by $.activePage. When action of controller is done, I should trigger the create
event on document to get 'enhanced' by $.mobile newly created elements.
I did this by replacing loadUrl
Backbone.history.loadUrl = ( function( old ){
return function() {
Router.trigger("all:before");
old.apply( Backbone.history, arguments );
Router.trigger("all:after" );
}
})( Backbone.history.loadUrl );
//Router.initialize
initialize: function() {
this.bind( "all:before", function( ) {
$.mobile.changePage( window.location.hash.split( "/" )[0] || "home", { changeHash:false} );
});
this.bind( "all:after", function() {
$.mobile.activePage.trigger('create');
});
}
Is there maybe some built-in events like this?
Share Improve this question edited Jan 19, 2012 at 7:50 abuduba asked Jan 19, 2012 at 7:41 abudubaabuduba 5,0527 gold badges28 silver badges43 bronze badges 1-
1
Off topic, but I love the way you wrapped the
Backbone.history.loadUrl
function. Really clean! – surj Commented Dec 15, 2012 at 1:48
1 Answer
Reset to default 9there is no before and after event in the Backbone.Router as of now.
version 0.5.3 supports the following router events (all events here)
"route:[name]" (router) — when one of a router's routes has matched.
not sure for which release it will be, but a general route event is being developed, in the github trunk mit #419 has this functionality, but it's not released in a new version yet
there are allready reported issues (idea's) for a before event, check the github issue on that here.
so, the before event is not yet in there, the after event will e some time later, if you really already need an after event, there is a way you can put it in yourself, by binding to the 'all' event instead, it catches all events including all 'route:routename' events. then just split the event name and if it starts with route you have the general route event. (this can of course be removed and changed if the after event is released in one of backbone's releases)
this.bind('all', function (trigger, args) {
var routeData = trigger.split(":");
if (routeData[0] === "route") {
// do whatever here.
// routeData[1] will have the route name
}
});
本文标签: javascriptBackbone router eventsStack Overflow
版权声明:本文标题:javascript - Backbone router events - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743791327a2539626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论