使用ember数据保存到hasMany关系的正确方法 [英] Proper way to save to a hasMany relationship using ember data

查看:82
本文介绍了使用ember数据保存到hasMany关系的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 应用程序。 List = DS.Model.extend({
listName:DS.attr(),
卡:DS.hasMany('card',{async:true})
});

  App.Card = DS.Model.extend({
description:DS.attr(),
list:DS.belongsTo('list')
});

我使用以下代码保存模型并将其添加到hasMany关系。

  createCard:function(){
var list = this.get('model'),
卡片

card = this.store.createRecord('card',{
description:this.get('cardDescription'),
list:list
});

card.save()然后(function(){
var cards = list.get('cards');

cards.then(function ){
cards.pushObject(card);
list.save();
});
});

this.set('cardDescription','');
}

保存hasMany集合的父项时,我遇到间歇性问题。有时,这些卡可以正确添加到列表集合中(列表中有一组卡片ID),有时卡片不正确地添加(列表中有一组卡片对象),有时候,这些关系会一起丢失(列表中不包含卡阵列)。



这些症状导致我认为它是一个异步问题,或者当我保存对象时我正在使用诺言。

解决方案

这看起来和我一直使用这里。我唯一的区别是我把孩子的模型推到了父母的一步(我怀疑有所作为),并且还有评论(我猜这是你的情况下的卡)作为承诺的内容'then'功能,这可能会有所作为?

  var post = this.get('controllers.post.content' ); 
var comment = this.get('store')。createRecord('comment',{post:post,text:this.get('text')});
comment.save()。then(function(comment){
post.get('comments')。pushObject(comment);
});


I have some related models in my ember.js app (using Ember 1.0 and EmberData 1.0 RC2):

App.List = DS.Model.extend({
    listName : DS.attr( ),
    cards : DS.hasMany( 'card', { async : true } )
});

and

App.Card  = DS.Model.extend({
    description : DS.attr(  ),
    list : DS.belongsTo( 'list' )
});

I'm using the following code to save models and add them to a hasMany relationship.

createCard : function(){
    var list = this.get( 'model' ),
        card ;

    card = this.store.createRecord( 'card', {
        description : this.get( 'cardDescription' ),
        list : list
    } );

    card.save().then( function(){
        var cards = list.get( 'cards' );

        cards.then( function(){
            cards.pushObject( card );
            list.save();
        } );
    } );

    this.set( 'cardDescription', '' );
}

I'm running into intermittent issues when saving the parent of the hasMany collection. Sometimes the cards get added to the lists collection properly ( the lists has an array of card id's) and sometimes the cards get added incorrectly ( the lists has an array of card objects) and sometimes the relationship gets lost all together (the lists contains no array of cards).

These symptoms lead me to think that its an async issue or that i'm using the promises incorrectly when saving the objects.

解决方案

This looks pretty much the same as I have used here. The only difference I can see is that I pushed the child model to the parent in one step (which I doubt makes a difference) and also had the comment (which I guess is the card in your case) as the subject of the promise inside the 'then' function, maybe that makes a difference?

var post = this.get('controllers.post.content');
var comment = this.get('store').createRecord('comment', { post: post, text: this.get('text') });
comment.save().then(function(comment){
  post.get('comments').pushObject(comment);
});

这篇关于使用ember数据保存到hasMany关系的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆