2016-04-15 5 views
0

Ember Dataと協力して、それぞれの割引が適用されたストア内のすべての商品の合計と等しい計算されたプロパティを作成しようとしています。私は連鎖を約束するために新しいです、そして、私はこの連鎖をどのようにフォーマットしているのかという問題であると信じています。Ember Promiseチェーンが返されない合計

export default DS.Model.extend({ 
title: DS.attr('string'), 
storeProducts: DS.hasMany('storeProduct',{async:true}), 
totalStoreValue:function(){ 
store.get('storeProducts').then(function(storeProducts){ //async call 1 
    return storeProducts.reduce(function(previousValue, storeProduct){ //begin sum 
    return storeProduct.get('product').then(function(product){ //async call 2 
     let price = product.get('price'); 
     let discount = storeProduct.get('discount'); 
     return previousValue + (price * discount); 
    }); 
    },0); 
}).then(function(result){ // 
    return result; 
}); 
}.property('[email protected]'), 

ご協力いただきありがとうございます。

答えて

1

使用Ember.RSVP.all合計を計算する前に約束のリストを解決するために:

store.get('storeProducts').then((storeProducts) => { //async call 1 
    return Ember.RSVP.all(storeProducts.map(storeProduct => { 
    return storeProduct.get('product').then((product) => { //async call 2 
     let price = product.get('price'); 
     let discount = storeProduct.get('discount'); 
     return price * discount; 
    }); 
    })); 
}).then(function(result){ // 
    return result.reduce((prev, curr) => { 
    return prev+curr; 
    }, 0); 
}); 
+0

ちょうど私が必要なもの。ありがとうございます! –

関連する問題