最新の Ember Data バージョン 1.0.0-beta を使用しています。オブジェクトをローカル ストレージの hasMany 関係に追加しようとしていますが、問題が発生しています。
私は2つのモデルを持っています:
App.Item = DS.Model.extend({
description: DS.attr('string'),
});
App.Basket = DS.Model.extend({
selectedItems: DS.hasMany('item'),
name: DS.attr('string'),
})
説明文字列の配列を含む「Items_descriptions」という配列があります。次の方法でアイテムをバスケットに挿入しようとしています。
var basket_record = App.Basket.store.createRecord('basket', { name: 'Test_record' });
var itemsarray = [];
Items_descriptions.forEach(function(description){
var item = App.Item.store.filter('item', function(record){return record.get('description') == description}).objectAt(0);
itemsarray.push(item);
});
basket_record.get('selectedItems').pushObjects(itemsarray); // ***errors out here****
basket_record.save();
ただし、この行basket_record.get('selectedItems').pushObjects(itemsarray);"で次のエラーが発生します。
キャッチされていない TypeError: 未定義のプロパティ 'typeKey' を読み取ることができません
App.Model.store.find メソッドも使用してみましたが、それも機能しません。
var basket_record = App.Basket.store.createRecord('basket', { name: 'Test_record' });
var itemsarray = [];
Items_descriptions.forEach(function(description){
App.Item.store.find('item', {'description': description}).then(function(item){
itemsarray.push(item);
});
});
basket_record.get('selectedItems').pushObjects(itemsarray);
basket_record.save();
App.Mode.store.find メソッドは機能しますが、データが selectedItems 属性に正常に追加されません。localstorage を確認すると、バスケットの name 属性は正しく入力されていますが、選択したアイテムの配列は空です。
私が理解していることから、 App.Model.store.find は promise を返し、 App.Model.store.filter().objectAt(0) はオブジェクト自体を返します。
私がここで間違っていることは何か分かりますか? どんな助けでも大歓迎です。本当にありがとう!