簡體   English   中英

灰燼數據-具有hasMany關系的createRecord

[英]Ember Data- createRecord in a hasMany relationship

我正在使用Ember Data beta2,並且已經建立了hasMany關系。

創建子記錄時,是否必須在父項的相應屬性上使用pushObject?

當查看文檔時,我會覺得需要正確設置記錄的父屬性並保存它。

這是我的方法:

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

我在Chrome的Ember檢查器中看到了新創建的記錄,它並不臟,但是在父列表中不存在,刷新后就消失了。

對我有用的是以下內容:

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

我不知道addPlugin屬於什么,但是如果要從ChildrenArrayController創建子代,則可能要包括

needs: ['parent']

在您的控制器中。 在創建子代並將其添加到父代之前,您需要調用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM