Alexandre Mélard
Alexandre Mélard

Reputation: 12679

ember-data find using a model fetched using other 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:

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

Upvotes: 2

Views: 170

Answers (1)

Alexandre Mélard
Alexandre Mélard

Reputation: 12679

I figured out after some hours of sleep (as usual...)

The problem was that store.find returns an Ember.Enumerable and not a Record. Therefore, you need to iterate trough the results in order to get the proper DS.Model objects. In my case, I needed only one record so I am using the first object.

here is the fix:

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

Upvotes: 2

Related Questions