我試圖包括應用程序實例來使用它的事件聚合器如圖所示hereBackbone Marionette:Marionette.Application導致Require.js模塊加載錯誤,「'錯誤:模塊名'App'尚未加載上下文:_」
我在包含實例的視圖中出現錯誤。
在Requirejs配置文件踢東西了,從App.Bootloader.js:
require(['App'], function (App){
App.start();
});
從App.js:
define(function (require){
//...requisite includes $, _, Backbone, Marionette ...
var Layout = require('Layout');
var App = new Marionette.Application();
App.addRegions({
main: '#view_content'
});
App.addInitializer(function(){
App.main.show(new Layout());
//... adding router etc ...
Backbone.Marionette.TemplateCache.loadTemplate = function (template, callback){
callback.call(this, Handlebars.compile(template));
};
Backbone.history.start();
});
return App;
});
從Layout.js:
define(function(require){
var View = require('folder/folder/View');
//template contains #sub div
var template = require('text!template.html');
return Marionette.Layout.extend({
template: template,
regions: {
sub: '#sub'
},
initialize: function(){
//wait till template is rendered in dom
_.defer(function(region){
region.sub.show(new View());
}, this)
}
});
});
從/folder/folder/View.js:
define(function (require){
//...requisite includes $, _, Backbone, Marionette ...
var App = require('App');
return Marionette.ItemView.extend({});
});
我在哪裏得到的錯誤「「錯誤:模塊名稱‘應用’尚未加載方面:_」
任何想法?讓我知道你是否需要更多的信息。
是的,你對錯字是正確的,它是固定的。我現在沒有看到循環依賴:應用程序需要佈局,佈局顯示視圖,視圖調用應用程序,因爲在Marionette中,應用程序具有用於交叉視圖模塊觀察的寶貴事件聚合器(即App.vent.trigger或App.vent.bind)又名酒吧子。 –
臨時攻擊是將應用程序導出到window.App以供其他人訪問它,但這不應該是必需的,並打破了包含模塊的哲學。 –
好的,您可以使用[特殊模塊'exports'] [1]。例如:http://stackoverflow.com/questions/4881059/how-to-handle-circular-dependencies-with-requiesjs-amd 1:https://github.com/jrburke/requirejs/wiki/Differences-between -the-simplified-CommonJS-wrapper-and-standard-AMD-define#wiki-magic – theotheo