Ember Data- createRecord在hasMany关系中 [英] Ember Data- createRecord in a hasMany relationship

查看:93
本文介绍了Ember Data- createRecord在hasMany关系中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



创建子记录时,我必须在父对应的属性上使用pushObject吗?



查看文档时,我得到的印象是我需要正确设置记录的父属性并保存。



这是我如何做的:

  addPlugin:function(){
//获取值
var title = this.get('newPluginName');
if(!title.trim()){return; }

var plugin = {
name:title,
category:this.get('model'),
url:''
};
var plugin = this.store.createRecord('plugin',plugin);
plugin.save();

//清除文本字段
this.set('newPluginName','');
$(#new-plugin)。blur();
}

我在Chrome中的Ember检查器中看到新创建的记录,但不是肮脏,但它不存在于父列表中,并且在我刷新之后它已经消失。

解决方案

对我有用的是以下:

  var child = this.get('store')。createRecord('child',{
name :'New Child',
parent:parent
};
child.save();

parent.get('children')。addObject(child) ;
//当您保存
//子代时,我的后端会自动添加关系的倒数,因此我不需要单独保存父项

我不知道什么 addPlugin 属于,但如果您正在创建一个ChildrenArrayController的孩子可能要包含

 需要:['parent'] 

在您的控制器中,然后创建孩子并将其添加到您将需要调用的父级:

  var parent = this.get('controllers.parent'); 


I am using Ember Data beta2 and I have a hasMany relationship set up.

When creating a child record do I have to use pushObject on the parent's corresponding property?

When looking at the documentation I get the impression that I need to correctly set the record's parent property and save it.

This is how I do it:

    addPlugin: function() {
        //get the value
        var title = this.get('newPluginName');
        if (!title.trim()) { return; }

        var plugin = {
            name: title,
            category: this.get('model'),
            url: ''
        };
        var plugin = this.store.createRecord('plugin', plugin);
        plugin.save();

        //clear the text field
        this.set('newPluginName', '');
        $("#new-plugin").blur();
    }

I see the newly created record in the Ember inspector in Chrome, it is not dirty, but it is not present in the parent listing and after I refresh it is gone.

解决方案

What works for me is the following:

var child = this.get('store').createRecord('child', {
   name: 'New Child',
   parent: parent
};
child.save();

parent.get('children').addObject(child);
// My backend automatically adds the inverse of relationships when saving     
// the child, so I don't need to save the parent separately

I don't know what addPlugin belongs to, but if you are creating the children from a ChildrenArrayController you may want to include

needs: ['parent']

in your controller. And before creating the child and adding it to the parent you will need to call:

var parent = this.get('controllers.parent');

这篇关于Ember Data- createRecord在hasMany关系中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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