对于一个Web应用程序循环依赖使用backbone.marionette和requireJs [英] Circular Dependencies for a web app using backbone.marionette and requireJs
问题描述
我在下面的情况。
我使用requireJs到负荷模块,我不希望使用全局变量。
I am using requireJs to loads module and I don't want to use global variables.
在main.js负责加载路由器。结果
然后路由器加载应用程序和应用程序加载几个subApps。
The main.js is responsible to load the router.
Then the router loads the app and the app loads several subApps.
一切都已经被初始化后,subApps需要路由器
制作 router.navigate
。
After everything has been initialised, the subApps needs the router
for making router.navigate
.
下面的模式:
main.js -> router -> app -> subApp -> router
然后,我有循环依赖的问题,为此,在subApp路由器将是不确定的。
Then I have a problem of Circular Dependencies and for that reason the router in subApp will be undefined.
什么是重新安排我的code或来解决这个问题的最好方法是什么?
是否有一些有关此例子吗?
What is the best way to reorganise my code or to fix this problem? Are there some example about this?
推荐答案
的模式:
main.js -> router -> app -> subApp -> router
是正确的。
如果您使用的是<一个href=\"https://github.com/derickbailey/backbone.marionette/blob/master/docs/marionette.application.md\">backbone.marionette,为了从应用程序和subApp访问路由器,而无需使用全局变量
,你应该开始应用在路由器是这样的:
If you are using backbone.marionette, in order to access the router from the app and subApp, without using global var
, you should start the app in router in this way:
// router.js
YourApp.start(router: router);
// app.js
YourApp.addInitializer(function(options){
// do useful stuff here
var myView = new MyView({
router: options.router
});
YourApp.mainRegion.show(myView);
});
这篇关于对于一个Web应用程序循环依赖使用backbone.marionette和requireJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!