data相关联的模型

data相关联的模型

本文介绍了删除与ember-data相关联的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:

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

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

当用户被删除时,它也会删除其后端的所有评论,所以我应该删除他们从客户端身份图。



我从另一个地方列出了系统上的所有评论,所以在删除用户之后,它将会崩溃。



有没有办法在关联中指定这种依赖关系?谢谢!

解决方案

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

  App.Post = DS.Model.extend(App.DeletesDependentRelationships,{
依赖关系:['comments'],

评论:DS.hasMany('App.Comment'),
作者: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({

//要删除的关系名称数组
dependRelationships:null,

//设置为删除或卸载,具体取决于您是否要
//实际发送删除到服务器
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 relatio nshipType = 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;

//从RecordArray中删除不适用于forEach,
//所以首先转换为正常数组
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);
}
}
});

请注意,您可以通过 deleteMethod 指定或者您不想将 DELETE 请求发送到您的API。如果您的后端配置为自动删除相关记录,那么您将需要使用默认值。



这是一个,表示它在行动。


I have two models:

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

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

When a user is deleted, it also will delete all its comments on the backend, so I should delete them from the client-side identity map.

I'm listing all the comments on the system from another place, so after deleting a user it would just crash.

Is there any way to specify this kind of dependency on the association? Thanks!

解决方案

I use a mixin when I want to implement this behaviour. My models are defined as follows:

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')
});

The mixin itself:

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);
        }
    }
});

Note that you can specify via deleteMethod whether or not you want to send the DELETE requests to your API. If your back-end is configured to delete dependent records automatically, then you will want to use the default.

Here's a jsfiddle that shows it in action.

这篇关于删除与ember-data相关联的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:20