0
我遵循this模式来组织我的js应用程序。backbone.js问题模块定义
正如该例所说,我们的应用程序应该有单一的入口点。文件application.js这样做的工作。
// Filename: application.js
var chat = {
// Create this closure to contain the cached modules
module: function() {
// Internal module cache.
var modules = {};
// Create a new module reference scaffold or load an
// existing module.
return function(name) {
// If this module has already been created, return it.
if (modules[name]) {
return modules[name];
}
// Create a module and save it under this name
return modules[name] = { Views: {} };
};
}()
};
// Using the jQuery ready event is excellent for ensuring all
// code has been downloaded and evaluated and is ready to be
// initialized. Treat this as your single entry point into the
// application.
jQuery(function($) {
$(document).ready(function(){
var foo = new Application.module('Chat').Collection();
});
});
// Filename: chat-module.js
(function(chat){
chat.Model = Backbone.Model.extend({ ... }),
chat.Collection = Backbone.Collection.extend({ ... }),
})(Application.module('Chat'));
这似乎很好,但如果试图定义聊天模块例如后来调用它,我有以下错误:
我认为错误是由于jQuery的准备时将调用聊天module.js还不可用。 我该如何解决这个问题?
对不起,我的错误我的应用程序名称是“聊天”。我有以下一系列脚本加载:1. application.js 2. chat-module.js。因此,当chat-module.js不可用时,jQuery准备好调用。但是我从上面提到的教程中获得了这个例子。 – Erik 2012-02-05 11:27:26
@Erik:我想你已经误解了我的观点。在'application.js'中,你正在创建一个名为'chat'的变量,而不是'Application'。如果将'var chat = ...'更改为'var Application = ...',它可能会清除您的问题。 – 2012-02-05 11:42:21