How can I get a list of models from the collection that have changes since constuction

为君一笑 提交于 2019-12-11 08:16:37

问题


If I initialize a collection with an array of objects for the model attributes when a dialogview is initialized. Then the dialog view that lets the user edit the list updates those model values with calls to model set. When the dialog's ok button is clicked, Does backbone provide a way to get the list of only those models that changed since the collection was created/initialized?


回答1:


There are various model methods that look tempting:

  • Model#hasChanged
  • Model#changedAttributes
  • Model#previous
  • Model#previousAttributes

But don't be fooled, those only apply while a "change" event is being triggered:

Note that this method, and the following change-related ones, are only useful during the course of a "change" event.

so they're useless after the event has been triggered and handled.

I think you have to track which models have changed yourself. You can do this on the collection itself without too much effort since

Any event that is triggered on a model in a collection will also be triggered on the collection directly, for convenience.

and the collection can bind to its own events. For example, you could have something like this in your collection:

Backbone.Collection.extend({
    initialize: function() {
        this.delta = { };
        this.on('change',​​​​​ this._a_model_has_changed);
    },
    changed_models: function() {
        return _.chain(this.delta).values();
    },
    _a_model_has_changed: function(m) {
        this.delta[m.id] = m;
    }
});

Then you could get the models that have changed by calling collection.changed_models(). You'd want to listen for other events as well so that you could update this.delta when models are deleted or synced with the server; the above is just for illustration. If you didn't want an Underscore object returned you could use this instead:

changed_models: function() {
    return _(this.delta).values();
}

but being able to collection.changed_models().each(function() { ... }) is convenient.

Demo: http://jsfiddle.net/ambiguous/8PQh9/

You could also let the models track their own dirtiness through a similar set on the models. Then you could do something like this:

collection.filter(function(m) { return m.is_dirty() });

where, of course, is_dirty would return true if the model had been changed.



来源:https://stackoverflow.com/questions/10220631/how-can-i-get-a-list-of-models-from-the-collection-that-have-changes-since-const

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!