【发布时间】:2012-11-14 18:49:18
【问题描述】:
如果我有一个对象集合,但还想存储有关这些对象的一些更高级别的信息,是否适合向集合中添加一些模型行为?
在我的情况下,我正在寻找一组应用程序路径来拥有一个名为“curPath”的布尔字段。如果已更改,则该集合应设置一个指示当前页面的标志。这样外部观察者只需要观察一个字段,而不是路径集合中的每个模型。
这可能是这样的:
var PathModel = Backbone.Model.extend({})
var PathCollection = Backbone.Collection.extend({
initialize: function(){ this.model = PathModel }
})
// I want to be able to set observable properties on the collection, so...
var PathManager = _.extend(Backbone.Model, PathCollection)
// Then maybe I can do something like this?
PathManager.each(function(pathModel){
pathModel.on('change:curPath', function(m, value, o){
// I mean for 'this'.set to point to the instance of the PathManager
if (value === true){ this.set('curPath', pathModel.get('id')) }
}, this)
}, this)
是否适合将可观察行为添加到集合(集合+模型>模型),还是需要将包装模型添加到整个事物(模型>集合>模型),还是有其他解决方案?
【问题讨论】:
标签: javascript architecture backbone.js publish-subscribe