多个系列中的模特?

发布于 2024-12-14 04:24:38 字数 1125 浏览 0 评论 0原文

我有点迷失所以任何帮助将不胜感激。 (我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-12-21 04:24:38

当这个模型被批量改变时,它会更新MasterCollection中的模型吗?

因为您这样做

batch.add(@toJSON)

实际上只是将模型的克隆添加到 batch 集合中。因此,当您更改该集合的模型属性时,原始模型不会受到影响。

当然,这些都是浅拷贝,因此如果您执行类似的操作,

(batch.at(0).get 'attr').x = y

将会修改原始文件的 attr 属性。 (您也不会触发任何更改事件。)一般来说,这是 Backbone 的禁忌。相反,做类似的事情

attrCopy = _.extend {}, batch.at(0).get 'attr'
attrCopy.x = y
batch.at(0).set attr: attrCopy

When this model is changed by the batch it will update the model in the MasterCollection?

Since you're doing

batch.add(@toJSON)

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

(batch.at(0).get 'attr').x = y

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

attrCopy = _.extend {}, batch.at(0).get 'attr'
attrCopy.x = y
batch.at(0).set attr: attrCopy
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文