0
votes

Here's my Ember-Data model:

Lrt.Option = DS.Model.extend({
    option_relation_value:  hasMany('option')
});

Here is an example of the JSON: (Shortened for the sake of this question)

{
    "optionGroups": [], 
    "optionSubGroups": [
        {
            "id": "3",  
            "optionType": [
                "80", 
                "81", 
                "82", 
                "83", 
                "84", 
                "248", 
                "278"
            ],
            "title": "Option Group for 80"
        }
    ], 
    "options": [
        {
            "id": "45", 
            "option_relation_value": [
                "80"
            ]
        },
        {
            "id": "80",
            "option_relation_value": []
        }
    ]
}

There are also "OptionGroup" and "OptionSubGroup" models which are sideloading options in.

The issue I'm having is that after adding in the 'hasMany' into the model, I can no longer do query the store for Options like this:

this.get('store').find('option')

It simply returns "0", however in the Ember Inspector, I get 400+ entries so I know the data loaded.

When using the chrome inspector and break on ALL Exceptions, it breaks on line 2246 of Ember-Data at the following line:

2246: Ember.assert('The id ' + id + ' has already been used with another record of type ' + type.toString() + '.', !id || !idToRecord[id]);

The error is:

"Cannot call method 'toString' of undefined"

"type" in this line is 'undefined'.

What am I doing wrong with this hasMany relationship?

I am using Ember-Data 1.0 Beta 2.

1

1 Answers

0
votes

Technically, this is not side loading, it's really more like nested loading. That may have something to do with it.

For true side loading you'd want a structure like this as your outer SON response :

{
  "option" : {
    "id" : 3,
    ...
    "options" : [45,80]
  }
  "options" : [
    { "id":45 , ... },
    { "id":80 , ... }
  ]
}

Here are the docs about JSON conventions : http://emberjs.com/guides/models/connecting-to-an-http-server/#toc_json-conventions The comments are an example of side loading.

I know that this works for separate model structures (post -> comment), but I'm not sure about self referential tree type structures.