多个系列中的模特?
我有点迷失所以任何帮助将不胜感激。 (我正在使用 Backbone.js 和 CoffeeScript。)
我有一组模型。它们都放在MasterCollection
中。
MasterCollection extends Backbone.Collection
model: Model
MasterCollection.add({#attributes of a new model})
我有时需要分离这些模型并批量处理它们的属性。这些批次还需要有一个相应的 DOM 视图来显示所有模型的数据。
Model extends Backbone.Model
initialize: () ->
#add the model to it's batch, batches are collections stored in an array
batches = ParentModel.get('baches')
#find the batch this model belongs in
for batch in batches
if batch = #the right one
batch.add(@toJSON)
Batch extends Backbone.Collection
changeAttributes: () ->
for model in @models
#change things about the model
- 当这个模型批量改变时,会更新MasterCollection中的模型吗?
- 当我完成批量集合后,如何在不删除其模型的情况下摆脱它?
- 我应该将这些批量集合存储在比数组更好的东西中吗?她们应该是模特吗?
由于我需要 DOM 来绑定新批次的创建,因此将它们作为集合中的模型会很棒。
总的来说,这是做这类事情的好方法吗?
谢谢!
I'm kinda lost so any help would be much appreciated. (I’m using Backbone.js and CoffeeScript.)
I have a group of models. They are all put in MasterCollection
.
MasterCollection extends Backbone.Collection
model: Model
MasterCollection.add({#attributes of a new model})
I need to separate these models at times and process their attributes in batches. These batches also need to have a corresponding DOM view that can show all of the models’ data.
Model extends Backbone.Model
initialize: () ->
#add the model to it's batch, batches are collections stored in an array
batches = ParentModel.get('baches')
#find the batch this model belongs in
for batch in batches
if batch = #the right one
batch.add(@toJSON)
Batch extends Backbone.Collection
changeAttributes: () ->
for model in @models
#change things about the model
- When this model is changed by the batch, will it update the model in the
MasterCollection
? - When I’m done with a batch collection, how do I get rid of it without deleting its models?
- Should I store these batch collections in something better than an array? Should they be models?
Since I need the DOM to bind to the creation of new batches, having them as models in a collection would be great.
Is this a good way to do this type of thing overall?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您这样做
实际上只是将模型的克隆添加到
batch
集合中。因此,当您更改该集合的模型属性时,原始模型不会受到影响。当然,这些都是浅拷贝,因此如果您执行类似的操作,
将会修改原始文件的
attr
属性。 (您也不会触发任何更改事件。)一般来说,这是 Backbone 的禁忌。相反,做类似的事情Since you're doing
you're really just adding a clone of the model to the
batch
collection. So, when you change that collection's model's attributes, the originals won't be affected.Of course, these are shallow copies, so if you do something like
you will be modifying the
attr
attribute of the original. (You also won't trigger any change events.) This is a no-no with Backbone in general. Instead, do something like