Reputation: 1765
App.CardCategory = DS.Model.extend
properties: DS.hasMany 'App.CardProperty'
App.CardProperty = DS.Model.extend
symbol: DS.attr 'string'
label: DS.attr 'string'
popover: DS.attr 'string'
unit: DS.attr 'string'
DS.RESTAdapter.configure 'plurals',
card_property: 'card_properties'
I can get both CardProperty
and CardCategory
via App.XXX.find() / find(n) but can't traverse through this relationship in Chrome console.
This:
App.CardCategory.find(1).get('properties').objectAt(1)
returns `undefined' whereas:
App.CardCategory.find(1).get('properties')
returns:
Class {type: undefined, store: Class, _changesToSync: Ember.OrderedSet, owner: Class, name: "properties"…}
I had always problems with getting data in the console so that I'd like to know what is the definitive way to do this.
In response to comment.
App.CardCategory.find(1).get('properties.length') returns
0. The response to
/card_categories/1` is following:
{
"card_category":{
"contains_new_purchase_slider":false,
"id":1,
"name":"Balance Transfer Cards",
"promo_card_id":3,
"promo_panel_active":true,
"promo_panel_card_teaser":"Maiores rerum quibusdam consectetur id culpa. Enim unde explicabo et quae",
"promo_panel_description":"Fugiat optio sint dolores non ut qui eveniet.",
"promo_panel_header":"Unde ut voluptates eos ea dolor rerum mollitia.",
"property_ids":[
4,
2,
5,
16
],
"slug":"balance-transfer-cards"
}
}
Upvotes: 3
Views: 328
Reputation: 1765
OK, finally I've figured it out. Firstly, CardCategory
should have cardProperties
key and the key in JSON should be named card_property_ids
. I think I could't use properties' because the model is named
App.CardProperty`. So unline other model fields, relations should be named after the model name. Here's the correct example:
App.CardCategory = DS.Model.extend
cardProperties: DS.hasMany 'App.CardProperty'
App.CardProperty = DS.Model.extend
symbol: DS.attr 'string'
label: DS.attr 'string'
popover: DS.attr 'string'
unit: DS.attr 'string'
DS.RESTAdapter.configure 'plurals',
card_property: 'card_properties'
{
"card_category":{
"contains_new_purchase_slider":false,
"id":1,
"name":"Balance Transfer Cards",
"promo_card_id":3,
"promo_panel_active":true,
"promo_panel_card_teaser":"Maiores rerum quibusdam consectetur id culpa. Enim unde explicabo et quae",
"promo_panel_description":"Fugiat optio sint dolores non ut qui eveniet.",
"promo_panel_header":"Unde ut voluptates eos ea dolor rerum mollitia.",
"card_property_ids":[
4,
2,
5,
16
],
"slug":"balance-transfer-cards"
}
}
Upvotes: 1
Reputation: 47367
Some quick details, hasMany and belongsTo aren't materialized immediately after a find.
Try a couple of things just to see if this is the case.
App.CardProperty.find(); // load the properties first
var cc = App.CardCategory.find(1);
var props = cc.get('properties');
propsLength = props.get('length');
props.forEach(function(prop){
console.log('prop exists here: ' + prop.get('label'));
}
If it is the case, you can always do
Ember.run.next({
// they will be materialized at this point
});
Upvotes: 2