我正在使用这个设置:
Ember : 1.10.0
Ember Data : 1.0.0-beta.16
jQuery : 1.11.2
ember-localstorage-adapter: 0.5.2
我设法使用ember-cli在我的数据存储中存储一些数据(Localstorage)
现在,我想检索数据。我的模型中有3个类:
mtg-item.js
name: DS.attr('string'),
material: DS.attr('string'),
description: DS.attr('string')
mtg-point.js
long: DS.attr('string'),
lat: DS.attr('string')
mtg-item-at-point.js
item: DS.belongsTo('mtgItem', {inverse: null}),
position: DS.belongsTo('mtgPoint', {inverse: null})
以下是localstorage中的数据:
mantrailling-item: "{"mtgItem":{"records":{"an0jf":{"id":"an0jf","name":"chaussette","material":"tissu","description":"carré de tissus"}}}}"
mantrailling-item-at-point: "{"mtgItemAtPoint":{"records":{"r7v07":{"id":"r7v07","item":"an0jf","position":"qqnpa"}}}}"
mantrailling-point: "{"mtgPoint":{"records":{"qqnpa":{"id":"qqnpa","long":"0","lat":"0"}}}}"mantrailling-style: "{"mtgStyle":{"records":{"rggrm":{"id":"rggrm","name":"default","path":null}}}}"__proto__: Storage
当我尝试检索数据时,检索mtgItem和mtgPoint没有问题。 问题是尝试检索mtgItemAtPoint时。 我得到一个断言错误:
错误:断言失败:您无法将“未定义”记录添加到 'mtgItemAtPoint.item'。您只能为此添加“mtgItem”记录 关系。
调试时,我发现在尝试设置mtgItem时发生了这种情况。 我缩小了belongs-to.js文件行70中的搜索范围。
var type = this.relationshipMeta.type;
Ember.assert("You cannot add a '" + newRecord.constructor.typeKey + "' record to the '" + this.record.constructor.typeKey + "." + this.key +"'. " + "You can only add a '" + type.typeKey + "' record to this relationship.", (function () {
if (type.__isMixin) {
return type.__mixin.detect(newRecord);
}
if (Ember.MODEL_FACTORY_INJECTIONS) {
type = type.superclass;
}
return newRecord instanceof type;
})());
断言试图检查newRecord是否扩展了超类型DS.Model。
当我在debug中检索值时,这是我得到的类型和newRecord:
newRecord.type.__super__.constructor
(subclass of DS.Model)
type
(subclass of DS.Model)
所以我不明白为什么要这样做:
return newRecord instanceof type
返回false?
为了记录,我打电话给这样的发现:
var mtgItem = store.find('mtgItem', {name: "chaussette", material: "tissu"});
mtgItem.then(function(mtgItem) {
var mtgPoint = store.find('mtgPoint', {long: "0", lat: "0"});
mtgPoint.then(function(mtgPoint) {
var mtgItemAtPoint = store.find('mtgItemAtPoint', {item: mtgItem, position: mtgPoint});
});
});
答案 0 :(得分:2)
经过几个小时的睡眠后我想通了(像往常一样......)
问题是store.find返回Ember.Enumerable而不是Record。因此,您需要迭代结果才能获得正确的DS.Model对象。就我而言,我只需要一个记录,所以我使用的是第一个对象。
这是修复:
var mtgItems = store.find('mtgItem', {name: "chaussette", material: "tissu"});
mtgItems.then(function(mtgItems) {
var mtgItem = mtgItems.get("firstObject");
var mtgPoints = store.find('mtgPoint', {long: "0", lat: "0"});
mtgPoints.then(function(mtgPoints) {
var mtgPoint = mtgPoints.get("firstObject");
var mtgItemAtPoints = store.find('mtgItemAtPoint', {item: mtgItem, position: mtgPoint});
});
});