1

次の 2 つのモデルから関連付けられた関係を持つ新しいレコードを作成しようとしています。

App.Kid = DS.Model.extend({
    attribute1: DS.attr("string"),
    parent: DS.belongsTo(parent)
});

App.Parent = DS.Model.extend({
    attribute1: DS.attr("string"),
    kids: DS.hasMany(kid)
});

そして、私のルートは次のとおりです。フォームを介して属性の新しい値でモデルを保存するために、テンプレートでアクション ハンドラーも使用しています。

App.KidRoute = Ember.Route.extend({
    model: function (id) {
        return this.store.createRecord('kid', {parent: id});
    },
    actions:{
        save: function(){
            this.get('currentModel').save();
        }
    }
});

しかし、私はこのエラーが発生しています。

Assertion failed: You can only add a 'parent' record to this relationship 

私は何か間違ったことをしていることを知っていますがparent、属性だけで属している関係ではない場合に機能します。しかし、私はこれを望んでいません。

ありがとうございます!

4

1 に答える 1

2

そのコードでは:

this.store.createRecord('kid', {parent: id});

変数 id はおそらく何らかの文字列、数値などです。ember-data はモデル インスタンスを想定しているため、それをロードする必要があります。

以下を使用してみてください。

App.KidRoute = Ember.Route.extend({
    model: function (id) {
        var route = this;
        return this.store.find('parent', id).then(function(parentModel) {
            return route.store.createRecord('kid', {parent: parentModel});
        });
    },
    actions:{
        save: function(){
            this.get('currentModel').save();
        }
    }
});
于 2013-10-31T13:20:15.120 に答える