javascript 主干路由器事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8922557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:00:25  来源:igfitidea点击:

Backbone router events

javascriptbackbone.jsbackbone-events

提问by abuduba

Does in Backbone.Routerexists the events both before and after performing routing? My app works with jQuery.mobileand 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 createevent on document to get 'enhanced' by $.mobile newly created elements. I did this by replacing loadUrl

请问在Backbone.Router之前和执行路由后存在的事件?我的应用程序与jQuery.mobile$.mobile.changePage 一起工作并在执行路由之前需要调用 $.mobile.changePage 并且在控制器中可以通过 $.activePage 访问当前显示的页面。当控制器的动作完成时,我应该触发create文档上的事件以通过 $.mobile 新创建的元素“增强”。我通过替换来做到这一点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?

也许有一些像这样的内置事件?

回答by Sander

there is no before and after event in the Backbone.Router as of now.

截至目前,Backbone.Router 中没有前后事件。

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

0.5.3 版支持以下路由器事件(此处为所有事件

"route:[name]" (router) — when one of a router's routes has matched.

"route:[name]" (router) — 当路由器的路由之一匹配时。

not sure for which release it will be, but a general route event is being developed, in the github trunk commit #419has this functionality, but it's not released in a new version yet

不确定它将是哪个版本,但正在开发一个通用路由事件,在 github 主干提交#419 中具有此功能,但尚未在新版本中发布

there are allready reported issues (idea's) for a before event, check the github issue on thathere.

对于之前的事件,已经有报告的问题(想法),请在此处查看github 问题

so, the before event is not yet in there, the after event will come 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)

所以,before 事件还没有在那里,after 事件会在一段时间后到来,如果你真的需要一个 after 事件,你可以通过绑定到“all”事件来将它放入自己的方法中,它捕获所有事件,包括所有 'route:routename' 事件。然后只需拆分事件名称,如果它以路线开头,则您将获得一般路线事件。(如果 after 事件在主干版本之一中发布,这当然可以删除和更改)

    this.bind('all', function (trigger, args) {
               var routeData = trigger.split(":");
                if (routeData[0] === "route") {
                   // do whatever here.  
                   // routeData[1] will have the route name
                }
        });