问题 使用ember-data删除关联的模型


我有两个型号:

App.User = DS.Model.create({
  comments: DS.hasMany('App.Comment')
});

App.Comment = DS.Model.create({
  user: DS.belongsTo('App.User')
});

当用户被删除时,它也会删除后端的所有注释,因此我应该从客户端身份映射中删除它们。

我在其他地方列出了系统上的所有评论,因此在删除用户之后它就会崩溃。

有没有办法指定这种对关联的依赖?谢谢!


12265
2018-03-02 18:11


起源



答案:


当我想实现这种行为时,我使用mixin。我的模型定义如下:

App.Post = DS.Model.extend(App.DeletesDependentRelationships, {
    dependentRelationships: ['comments'],

    comments: DS.hasMany('App.Comment'),
    author: DS.belongsTo('App.User')
});

App.User = DS.Model.extend();

App.Comment = DS.Model.extend({
    post: DS.belongsTo('App.Post')
});

mixin本身:

App.DeletesDependentRelationships = Ember.Mixin.create({

    // an array of relationship names to delete
    dependentRelationships: null,

    // set to 'delete' or 'unload' depending on whether or not you want
    // to actually send the deletions to the server
    deleteMethod: 'unload', 

    deleteRecord: function() {
        var transaction = this.get('store').transaction();
        transaction.add(this);
        this.deleteDependentRelationships(transaction);
        this._super();
    },

    deleteDependentRelationships: function(transaction) {
        var self = this;
        var klass = Ember.get(this.constructor.toString());
        var fields = Ember.get(klass, 'fields');

        this.get('dependentRelationships').forEach(function(name) {
            var relationshipType = fields.get(name);
            switch(relationshipType) {
                case 'belongsTo': return self.deleteBelongsToRelationship(name, transaction);
                case 'hasMany': return self.deleteHasManyRelationship(name, transaction);
            }
        });
    },

    deleteBelongsToRelationship: function(name, transaction) {
        var record = this.get(name);
        if (record) this.deleteOrUnloadRecord(record, transaction);
    },

    deleteHasManyRelationship: function(key, transaction) {
        var self = this;

        // deleting from a RecordArray doesn't play well with forEach, 
        // so convert to a normal array first
        this.get(key).toArray().forEach(function(record) {
            self.deleteOrUnloadRecord(record, transaction);
        });
    },

    deleteOrUnloadRecord: function(record, transaction) {
        var deleteMethod = this.get('deleteMethod');
        if (deleteMethod === 'delete') {
            transaction.add(record);
            record.deleteRecord();
        }
        else if (deleteMethod === 'unload') {
            var store = this.get('store');
            store.unloadRecord(record);
        }
    }
});

请注意,您可以指定via deleteMethod 是否要发送 DELETE 对您的API的请求。如果您的后端配置为自动删除相关记录,那么您将需要使用默认值。

这是一个 的jsfiddle 这表明它在行动。


9
2018-03-03 03:10



嘿,这看起来非常好!虽然我觉得 ember-data 应该支持核心内容。谢谢你这么详细的解释,老兄! - josepjaume
我同意。 Ember Data缺少一些关键功能,但它仍然很年轻并且很快就会改进。 - ahmacleod
Ember Data不再支持事务,因此需要修改上面的代码以使用最新版本。 - ahmacleod


答案:


当我想实现这种行为时,我使用mixin。我的模型定义如下:

App.Post = DS.Model.extend(App.DeletesDependentRelationships, {
    dependentRelationships: ['comments'],

    comments: DS.hasMany('App.Comment'),
    author: DS.belongsTo('App.User')
});

App.User = DS.Model.extend();

App.Comment = DS.Model.extend({
    post: DS.belongsTo('App.Post')
});

mixin本身:

App.DeletesDependentRelationships = Ember.Mixin.create({

    // an array of relationship names to delete
    dependentRelationships: null,

    // set to 'delete' or 'unload' depending on whether or not you want
    // to actually send the deletions to the server
    deleteMethod: 'unload', 

    deleteRecord: function() {
        var transaction = this.get('store').transaction();
        transaction.add(this);
        this.deleteDependentRelationships(transaction);
        this._super();
    },

    deleteDependentRelationships: function(transaction) {
        var self = this;
        var klass = Ember.get(this.constructor.toString());
        var fields = Ember.get(klass, 'fields');

        this.get('dependentRelationships').forEach(function(name) {
            var relationshipType = fields.get(name);
            switch(relationshipType) {
                case 'belongsTo': return self.deleteBelongsToRelationship(name, transaction);
                case 'hasMany': return self.deleteHasManyRelationship(name, transaction);
            }
        });
    },

    deleteBelongsToRelationship: function(name, transaction) {
        var record = this.get(name);
        if (record) this.deleteOrUnloadRecord(record, transaction);
    },

    deleteHasManyRelationship: function(key, transaction) {
        var self = this;

        // deleting from a RecordArray doesn't play well with forEach, 
        // so convert to a normal array first
        this.get(key).toArray().forEach(function(record) {
            self.deleteOrUnloadRecord(record, transaction);
        });
    },

    deleteOrUnloadRecord: function(record, transaction) {
        var deleteMethod = this.get('deleteMethod');
        if (deleteMethod === 'delete') {
            transaction.add(record);
            record.deleteRecord();
        }
        else if (deleteMethod === 'unload') {
            var store = this.get('store');
            store.unloadRecord(record);
        }
    }
});

请注意,您可以指定via deleteMethod 是否要发送 DELETE 对您的API的请求。如果您的后端配置为自动删除相关记录,那么您将需要使用默认值。

这是一个 的jsfiddle 这表明它在行动。


9
2018-03-03 03:10



嘿,这看起来非常好!虽然我觉得 ember-data 应该支持核心内容。谢谢你这么详细的解释,老兄! - josepjaume
我同意。 Ember Data缺少一些关键功能,但它仍然很年轻并且很快就会改进。 - ahmacleod
Ember Data不再支持事务,因此需要修改上面的代码以使用最新版本。 - ahmacleod


一种快速而肮脏的方法是将以下内容添加到您的用户模型中

destroyRecord: ->
  @get('comments').invoke('unloadRecord')
  @_super()

3
2017-10-29 21:39





我调整了@ahmacleod的答案 ember-cli 2.13.1 和 ember-data 2.13.0。我遇到了嵌套关系的问题以及从数据库中删除实体后其ID被重用的事实。这导致与余烬数据模型中的残余冲突。

import Ember from 'ember';

export default Ember.Mixin.create({
    dependentRelationships: null,

    destroyRecord: function() {
        this.deleteDependentRelationships();

        return this._super()
        .then(function (model) {
            model.unloadRecord();

            return model;
        });
    },

    unloadRecord: function() {
        this.deleteDependentRelationships();

        this._super();
    },

    deleteDependentRelationships: function() {
        var self = this;
        var fields = Ember.get(this.constructor, 'fields');

        this.get('dependentRelationships').forEach(function(name) {
            self.deleteRelationship(name);
        });
    },

    deleteRelationship (name) {
        var self = this;
        self.get(name).then(function (records) {
            if (!records) {
                return;
            }

            var reset = [];
            if (!Ember.isArray(records)) {
                records = [records];
                reset = null;
            }

            records.forEach(function(record) {
                if (record) {
                    record.unloadRecord();
                }
            });

            self.set(name, reset);
        });
    },
});

最终,我不得不将关系设定为 [] (hasMany)或 null (属于)。否则我会遇到以下错误消息:

Assertion Failed: You cannot update the id index of an InternalModel once set. Attempted to update <id>.

也许这对其他人有帮助。


0
2018-06-04 08:32