Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 余烬数据查找使用使用其他查找获取的模型';s结果_Ember.js_Ember Data - Fatal编程技术网

Ember.js 余烬数据查找使用使用其他查找获取的模型';s结果

Ember.js 余烬数据查找使用使用其他查找获取的模型';s结果,ember.js,ember-data,Ember.js,Ember Data,我正在使用此设置: 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'), d

我正在使用此设置:

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时。 我得到一个断言错误:

错误:断言失败:无法将“未定义”记录添加到 “mtgitemapoint.item”。您只能将“mtgItem”记录添加到此 关系

调试时,我注意到它是在尝试设置mtgItem时发生的。 我缩小了搜索范围,在文件第70行显示了beans-to.js

  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中检索值时,以下是type和newRecord的结果:

newRecord.type.__super__.constructor
(subclass of DS.Model)

type
(subclass of DS.Model)
所以我不明白为什么会这样:

return newRecord instanceof type
返回false

为了记录在案,我这样称呼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,而不是记录。因此,您需要通过结果进行迭代,以获得正确的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});
    });
});