gpt4 book ai didi

ember.js - ember-data 使用使用其他查找结果获取的模型查找

转载 作者:行者123 更新时间:2023-12-02 06:08:31 25 4
gpt4 key购买 nike

我正在使用这个设置:

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

以下是本地存储中的数据:
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 时。
我收到一个断言错误:

Error: Assertion Failed: You cannot add a 'undefined' record to the 'mtgItemAtPoint.item'. You can only add a 'mtgItem' record to this relationship.



在调试时,我观察到它是在尝试设置 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 是否扩展了父类(super class)型 DS.Model。

当我在调试中检索值时,这是我得到的 type 和 newRecord:
newRecord.type.__super__.constructor
(subclass of DS.Model)

type
(subclass of DS.Model)

所以我不明白为什么会出现以下情况:
return newRecord instanceof type

返回假?

为了记录,我这样称呼 find :
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});
});
});

最佳答案

我睡了几个小时后才知道(像往常一样......)

问题是 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});
});
});

关于ember.js - ember-data 使用使用其他查找结果获取的模型查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29402349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com