mercredi 1 avril 2015

ember-data find using a model retrieve in another find's result

I am using this settup:



Ember : 1.10.0
Ember Data : 1.0.0-beta.16
jQuery : 1.11.2
ember-localstorage-adapter: 0.5.2


I managed to use the ember-cli to store some data in my datastore (Localstorage)


Now, I would like to retrieve the data. I have 3 classes in my model:



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


Here are the data in 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


when I try to retrieve the data, I have no problem retrieving the mtgItem and the mtgPoint. The issue is when trying to retrieve the mtgItemAtPoint. I get an assert error:



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



When debugging, I observed that it occured when trying to set the mtgItem. I narrowed the search in the belongs-to.js file line 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;
})());


The assertion is trying to check if the newRecord is of extends the supertype DS.Model.


When I retrieve the values in debug, here is what I get for type and newRecord:



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

type
(subclass of DS.Model)


So I don't get why the folowing:



return newRecord instanceof type


returns false?


For the record, I am calling the find like this:



mtgItem = store.find('mtgItem', {name: "chaussette", material: "tissu"});
mtgItem.then(function(mtgItem) {
mtgPoint = store.find('mtgPoint', {long: "0", lat: "0"});
mtgPoint.then(function(mtgPoint) {
mtgItemAtPoint = store.find('mtgItemAtPoint', {item: mtgItem, position: mtgPoint});
});
});




Aucun commentaire:

Enregistrer un commentaire