Router Events

Are there any events in Backbone.Router before or after routing? My application works with jQuery.mobile , and a call to $ .mobile.changePage was necessary before the route was completed, and the controller has access to the current page on the $ .activePage page. When the controller action is complete, I must fire the create event in the document to get "advanced" using the newly created $ .mobile 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'); }); } 

Perhaps there are such built-in events?

+4
source share
1 answer

Backbone.Router is not yet before and after the event.

version 0.5.3 supports the following router events ( all events are here )

"route: [name]" (router) - when one of the routes of the router matches.

I'm not sure what release it will be for, but a general route event is being developed, github trunk commit # 419 has this function, but it has not yet been released in the new version

any previously reported problems (ideas) for the event before this, check out the github problem on this here.

Thus, the previous event does not exist yet, the after event will happen after a while, if you really need the after event, there is a way that you can put it in yourself, instead of binding to the β€œall” event, it captures all events, including all events "route: routeename". then just split the name of the event, and if it starts with a route, you will have a general route event. (this can, of course, be deleted and changed if the after event is fired in one of the base 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 } }); 
+9
source

Source: https://habr.com/ru/post/1391787/


All Articles