主干路由器事件

发布于 2024-12-28 01:52:51 字数 854 浏览 5 评论 0原文

Backbone.Router 中是否存在执行路由之前和之后的事件? 我的应用程序与 jQuery.mobile 配合使用,在执行路由之前需要调用 $.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'); 
     });  
  }

是否可能有一些像这样的内置事件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2025-01-04 01:52:51

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

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

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

不确定它将是哪个版本,但正在开发一般路线活动,
在 github trunk 提交 #419 中有此功能,但尚未在新版本中发布,但

已经有报道之前事件的问题(想法),检查 github 问题在这里。

所以,之前的事件还没有在那里,之后的事件会在一段时间后出现,
如果你确实需要一个后续活动,有一种方法可以让你自己把它放进去,
通过绑定到“all”事件,它可以捕获所有事件,包括所有“route:routename”事件。然后只需拆分事件名称,如果它以路线开头,则您将获得一般路线事件。 (如果后事件在主干版本之一中发布,这当然可以被删除和更改)

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

there 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 commit #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 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)

    this.bind('all', function (trigger, args) {
               var routeData = trigger.split(":");
                if (routeData[0] === "route") {
                   // do whatever here.  
                   // routeData[1] will have the route name
                }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文