How to rollback relationship changes in EmberData

后端 未结 4 1079
春和景丽
春和景丽 2020-12-31 19:08

I have two models with parent-child relationship: training and exercise:

App.Training = DS.Model.extend({
  exercises:         


        
相关标签:
4条回答
  • 2020-12-31 19:29

    @tothda and other readers to follow. As of Ember Data : 1.0.0-beta.10+canary.7db210f29a the parent is still not designed to make parentTraining.isDirty() a value of true when a child is rolled back. Ember Data does consider a parent record to be dirty when an attribute is changed, but not when a DS.hasMany array has changes (this allows save() to work, so you can updated any changes to the parent's attributes on the server).

    The way around this for the case mentioned, where you want to do a rollback() on a newly created child, is to replace the .rollback() with a .deleteRecord() on the child record you want to discard. Ember Data then automatically knows to remove it from the DS.hasMany array then, and you can pat yourself on the back for a rollback well done!

    0 讨论(0)
  • 2020-12-31 19:32

    Late to the party, but here we go:

    I created an addon that resolves this issue. Just call rollbackRelationships() and it will rollback all your relationships (belongsTo & hasMany). Look at the README for more options.

    https://www.npmjs.com/package/ember-rollback-relationships

    0 讨论(0)
  • 2020-12-31 19:39

    A proper dirty check and rollback for hasMany and belongsTo relationships are sorely lacking in Ember Data. The way it currently behaves is often reported as a bug. This is a big pain point for a lot of developers and there is an ongoing discussion on how to resolve this here:

    https://github.com/emberjs/rfcs/pull/21

    Until there's a proper solution in place, you can workaround this problem by using the following approach.

    First, you'll want to reopen DS.Model and extend it. If you're using globals, you can can just put this (e.g. DS.Model.reopen({})) anywhere, but if you're using Ember CLI, it's best to create an initializer (e.g. ember g initializer model):

    import DS from 'ember-data';
    
    export function initialize(/* container, application */) {
    
        DS.Model.reopen({
    
            saveOriginalRelations: function() {
    
                this.originalRelations = {};
                this.constructor.eachRelationship(function(key, relationship) {
    
                    if (relationship.kind === 'belongsTo')
                        this.originalRelations[key] = this.get(key);
    
                    if (relationship.kind === 'hasMany')
                        this.originalRelations[key] = this.get(key).toArray();
    
                }, this);
            },
    
            onLoad: function() {
    
                this.saveOriginalRelations();
    
            }.on('didLoad', 'didCreate', 'didUpdate'),
    
            onReloading: function() {
    
                if (!this.get('isReloading'))
                    this.saveOriginalRelations();
    
            }.observes('isReloading'),    
    
            rollback: function() {
    
                this._super();
    
                if (!this.originalRelations)
                    return;
    
                Ember.keys(this.originalRelations).forEach(function(key) {
    
                    // careful, as Ember.typeOf for ArrayProxy is 'instance'
                    if (Ember.isArray(this.get(key))) {
                        this.get(key).setObjects(this.originalRelations[key]);
                        this.get(key).filterBy('isDirty').invoke('rollback');
                        return;
                    }
    
                    if (Ember.typeOf(this.get(key)) === 'instance') {
                        this.set(key, this.originalRelations[key]);
                        return;
                    }
    
                }, this);
            },
    
            isDeepDirty: function() {
                if (this._super('isDirty'))
                    return true;
    
                if (!this.originalRelations)
                    return false;
    
                return Ember.keys(this.originalRelations).any(function(key) {
    
                    if (Ember.isArray(this.get(key))) {
                        if (this.get(key).anyBy('isDirty'))
                            return true;
    
                        if (this.get(key).get('length') !== this.originalRelations[key].length)
                            return true;
    
                        var dirty = false;
                        this.get(key).forEach(function(item, index) {
                            if (item.get('id') !== this.originalRelations[key][index].get('id'))
                                dirty = true;
                        }, this);
    
                        return dirty;
                    }
    
                    return this.get(key).get('isDirty') || this.get(key).get('id') !== this.originalRelations[key].get('id');
    
                }, this);
            }
        });
    };
    
    export default {
        name: 'model',
        initialize: initialize
    };
    

    The code above essentially stores the original relationships on load or update so that it can later be used for rollback and dirty checking.

    model.rollback() should now roll back everything, including hasMany and belongsTo relationships. We still haven't fully addressed the 'isDirty' check though. To do that, we need to override isDirty in the concrete implementation of a model. The reason why we need to do it here and we can't do it generically in DS.Model is because DS.Model doesn't know what property changes to watch for. Here's an example using Ember CLI. The same approach would be used with globals, except that you'd assign this class to something like App.Book:

    import DS from 'ember-data';
    
    var Book = DS.Model.extend({
    
        publisher: DS.belongsTo('publisher'),
    
        authors: DS.hasMany('author'),
    
        isDirty: function() {
            return this.isDeepDirty();
        }.property('currentState', 'publisher', 'authors.[]', 'authors.@each.isDirty').readOnly()
    
    });
    
    export default Book;
    

    For the dependent arguments of isDirty, make sure to include all belongsTo relationships and also include 'array.[]' and 'array.@each.isDirty' for every hasMany relationship. Now isDirty should work as expected.

    0 讨论(0)
  • 2020-12-31 19:47

    This isn't pretty but you can force it to rollback by manually dirtying the parent record:

    parent.send('becomeDirty');
    parent.rollback();
    parent.get('children.length'); // => 0
    
    0 讨论(0)
提交回复
热议问题