我正在制作一个用于上传文件的Marionette应用程序,我就在它的最开始。首先,我将向您展示我正在使用的文件:
upload_view.js
AppManager.module("PrimaryApp.Upload", function(Upload, AppManager, Backbone, Marionette, $, _){
Upload.UploadPage = Marionette.ItemView.extend({
template: "#upload-template"
});
});
upload_controller.js
AppManager.module("PrimaryApp.Upload", function(Upload, AppManager, Backbone, Marionette, $, _){
Upload.Controller = {
show: function(){
var uploadView = new Upload.UploadPage();
AppManager.regions.primary.show(uploadView);
},
};
});
app.js
var AppManager = new Marionette.Application();
AppManager.on("before:start", function() {
var RegionContainer = Marionette.LayoutView.extend({
el: "#app-container",
regions: {
primary: "#primary-region",
secondary: "#secondary-region",
header: "#header-region"
}
});
AppManager.regions = new RegionContainer();
});
AppManager.on("start", function(){
console.log(AppManager);
AppManager.PrimaryApp.Upload.Controller.show();
})
AppManager.start();
在浏览器中运行此应用程序时出现此错误:
Uncaught TypeError: Cannot read property 'Upload' of undefined
对于app.js中的这行代码:
AppManager.PrimaryApp.Upload.Controller.show();
但是,当我将AppManager输出到控制台时,我得到了这个:
这表明AppManager.PrimaryApp确实已定义并包含所需的所有子模块。任何想法为什么我的AppManager.PrimaryApp在被调用为函数时未定义,但是在输出到控制台时定义了?
答案 0 :(得分:0)
弄清楚出了什么问题!
AppManager.start();
在错误的地方尝试在AppManager.PrimaryApp定义之前运行。但是,
console.log(AppManager);
在定义了AppManager.PrimaryApp之后,以某种方式调用,从而产生正确的输出。我的start()函数的简单移动修复了事物的顺序。