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'
我可以通过App.XXX.find()/ find(n)获取CardProperty
和CardCategory
,但无法在Chrome控制台中遍历此关系。
此:
App.CardCategory.find(1).get('properties').objectAt(1)
返回`undefined',而不是:
App.CardCategory.find(1).get('properties')
返回:
Class {type: undefined, store: Class, _changesToSync: Ember.OrderedSet, owner: Class, name: "properties"…}
我总是在控制台中获取数据时出现问题,因此我想知道执行此操作的最终方法是什么。
回应评论。
App.CardCategory.find(1).get('properties.length') returns
0 . The response to
/ card_categories / 1`如下:
{
"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"
}
}
答案 0 :(得分:2)
一些快速的细节,hasMany和belongsTo在查找后不会立即生效。
尝试一些事情,看看是否是这种情况。
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'));
}
如果是这种情况,您可以随时
Ember.run.next({
// they will be materialized at this point
});
答案 1 :(得分:1)
好的,终于我明白了。首先,CardCategory
应该有cardProperties
密钥,而JSON中的密钥应该命名为card_property_ids
。我想我不能使用properties' because the model is named
App.CardProperty`。因此,取消显示其他模型字段,关系应以模型名称命名。这是正确的例子:
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"
}
}