我有一个模型Item
,其中 id,title 在一个夹具中,其他数据来自对第三方的两次REST调用。
App.Item = DS.Model.extend({
title: DS.attr('string'),
buying: DS.attr('number'),
selling: DS.attr('number')
});
App.Item.FIXTURES = [
{
id: 1,
title: 'Cake',
},{
id: 2,
title: 'Shoes',
},{
id: 1,
title: 'Awesome stuff',
},
]
第三方给我这样的结果:
{
"result": [
{
"item": "Cake",
"result": 220
},
{
"item": "Shoes",
"result": 90
},
{
"item": "Awesome stuff",
"result": 100
}
]
}
我可以渲染视图,并在收到时以买/卖价格异步呈现吗?第三方为我提供了一个数字列表,所以我不想创建某种形式的把手模板或组件,因为在具有合理数量的项目的列表视图中,它将是额外的10-20个API调用。
答案 0 :(得分:1)
你绝对可以。没有技术障碍。这只是一种可行的方式。
model: function() {
var promise = this.get('store').find('item'),
pricePromise = Ember.$.getJSON('/someapi');
promise.then(function(items){
pricePromise.then(function(json){
//some black magic
var hash = {};
json.result.forEach(function(price){
hash[price.item] = price;
});
console.log(hash);
items.forEach(function(item){
var price = hash[item.get('title')];
if(price){
item.set('selling', price.result);
}
});
});
});
return promise;
}