为了回答你的问题,这是我的建议,回答所有三个:
模块应该尽可能小,所以我会为每个视图创建一个新模块,一个用于模块,一个用于序列化逻辑。然后我会创建一个将所有这些联系在一起的模块,这样外部代码就不必处理模型、视图或序列化。
这是我第一次尝试这样的事情:
// components/populationVisualization/Model.js
define(function (require, exports, module) {
return Backbone.Model.extend({ /* ... */});
});
// components/populationVisualization/views/Timeslider.js
define(function (require, exports, module) {
return Backbone.View.extend({ /* ... */});
});
// components/populationVisualization/views/Legend.js
define(function (require, exports, module) {
return Backbone.View.extend({ /* ... */});
});
// components/populationVisualization/serializer.js
define(function (require, exports, module) {
exports.setupSerialization = function (model) {
// ...
};
});
// components/populationVisualization.js
define(function (require, exports, module) {
var Model = require("./populationVisualization/Model");
var TimesliderView = require("./populationVisualization/views/Timeslider");
var LegendView = require("./populationVisualization/views/Legend");
var serializer = require("./populationVisualization/serializer");
exports.createAndRender = function (modelParams, $el) {
var model = new Model(modelParams);
serializer.setupSerialization(model);
var timesliderView = new TimesliderView({ model: model });
var legendView = new LegendView({ model: model });
$el.append(timesliderView.el);
$el.append(legendView.el);
};
});
在应用程序的其他地方,您只会使用适当的参数require("components/populationVisualization")
调用该模块的方法。createAndRender