您可以使用我所谓的需要js模块模式。如果你知道一组类经常一起使用,你可以做这样的事情。
首先,你在一个单独的文件中定义的每个类,然后你定义一个模块来结合在一起,
Module.js
define([
'./models/FirstModel',
'./models/SecondModel',
'./views/FirstView',
'./views/SecondView',
'txt!./templates/template.tpl'
], function(FirstModel, SecondModel, FirstView, SecondView, template) {
return {
FirstModel: FirstModel,
SecondModel: SecondModel,
FirstView: FirstView,
SecondView: SecondView,
template: template
}
});
然后当你想使用类从这个模块你只是做
define(['./Module'], function(Module) {
var AView = Module.FirstView.extend({
model: Module.FirstModel,
render: function() {
this.html(_.template(Module.template)(this.model.attributes));
if (something) {
this.$el.append(new Module.SecondView().render().el);
}
}
})
return AView;
});
我不相信使用requirejs定义的模块,我们应该返回一个实例 - 我们应该总是返回一个构造函数或一个对象。
你应该完全接受定义和要求 - 随着时间的推移,你会开始喜欢它 - 而不必考虑添加/跟踪依赖关系等等,无处不在手动或(所以2005年!)拥有大部分内容一个文件:)
绝对完全真棒。谢啦!我希望我能给你更多的声望...... – 2012-02-02 02:15:52
是帮助是足够的:) – 2012-02-02 12:17:56