骨干>多个路由器和History.start [英] Backbone > Multiple Routers and History.start

查看:133
本文介绍了骨干>多个路由器和History.start的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有多个路由器生活模块在一个页面上。我在初始化 $(文件)。就绪()在不同的js文件的路由器。当我有工作得很好,因为我可以打电话只是一个路由器 History.start()初始化路由器,但现在,我有可能会从不同的初始化多个路由器之后文件,我不知道什么时候调用 History.start()

I'd like to have multiple routers living on a single page for modularity. I initialize the routers on $(document).ready() in different js files. When I had just one router that worked fine because I could call History.start() right after initializing the router, but now that I have multiple routers that could be initialized from different files, I'm not sure when to call History.start().

例如:

<script src="router1.js" type="text/javascript"></script>
<script src="router2.js" type="text/javascript"></script>

在router1.js:

In router1.js:

$(document).ready(function(){
  new Core.Routers.Router1()
});

和同样为ROUTER2。

and likewise for router2.

是最好的解决办法只是增加一个新的 $(文件)。就绪()调用 History.start()在页面的结束?我不认为文档准备呼叫被阻塞,因此不认为引入中所有路由器都可能没有被时间 History.start()被调用。

Is the best solution just to add a new $(document).ready() that calls History.start() at the end of the page? I don't think the doc ready calls are blocking, so doesn't that introduce a race condition where all the Routers may not have been initialized by the time History.start() has been called.

推荐答案

您只需要在您的应用程序,唯一的标准 Backbone.history.start()调用一次当你要求它至少有一个路由器必须是已经实例化。

You only need to call Backbone.history.start() once in your app and the only criteria for when you call it is that at least one router has to be instantiated already.

所以,你可以很容易地做到这一点:

So, you could easily do this:


$(function(){
  new MyRouter();
  Backbone.history.start();
});


$(function(){
  new AnotherRouter();
});


$(function(){
  new AndMoreRouters();
});

我做类似的事情有定期的路由器,我经常启动的页面已经被加载后不久的新路由器和用户与网页互动。

I do a similar thing with routers on a regular basis, and I often start up new routers long after the page has been loaded and the user is interacting with the page.

FWIW不过,你可能会感兴趣的,我有我的Backbone.Marionette插件,并记录为这个博客帖子的一部分初始化的理念:<一href=\"http://lostechies.com/derickbailey/2011/12/16/composite-javascript-applications-with-backbone-and-backbone-marionette/\">http://lostechies.com/derickbailey/2011/12/16/composite-javascript-applications-with-backbone-and-backbone-marionette/

FWIW though, you might be interested in the idea of initializers that I have in my Backbone.Marionette plugin and documented as part of this blog post: http://lostechies.com/derickbailey/2011/12/16/composite-javascript-applications-with-backbone-and-backbone-marionette/

这篇关于骨干&GT;多个路由器和History.start的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆