2015-09-28 81 views
0

链无极如果我有它从来没有通过计算财产处理在计算财产

因此,这是正常解析正确承诺一则()函数,

taxRate: computed(function() { 
    return this.get('store').findRecord('tax-rate', { current: true }); 
    }) 

但这不是,(输出[object Object] - 承诺 - 在htmlbars模板)

taxRateValue: computed(function() { 
    return this.get('store').findRecord('tax-rate', { current: true }).then((taxRate) => { 
     return taxRate.get('taxRateValue'); 
    }) 
    }) 

那么,什么是正确的方式来处理这种不使用观测?

回答

0

第一个作品是因为Ember Data在内部使用PromiseObject。内部工作有点棘手,但其要点是当对象的承诺解决时,该对象触发观察者。第二个不起作用,因为你不再返回一个实现Ember对象系统的对象,你只是返回一个承诺。模板不知道如何处理承诺,这些都在对象逻辑中。至于你的问题:

那么什么是正确的方式来处理这个而不使用观察员?

恐怕这将是不可能的。您需要直接或间接使用某种观察者。我会建议使用一个直接以明确到底是怎么回事:

taxRateValue: null, 

taxRateValuePromise: observes('someProperty?', function() { 
    this.get('store').findRecord('tax-rate', { current: true }).then((taxRate) => { 
     this.set('taxRateValue', taxRate.get('taxRateValue')); 
    }) 
}), 

你可能也重新使用PromiseObject,但这需要你使用的对象,而不是一个值:

taxRateValue: computed(function() { 
    return DS.PromiseObject.create({ 
     promise: this.get('store').findRecord('tax-rate', { current: true }).then((taxRate) => { 
      return taxRate.get('taxRateValue'); 
     }) 
    }); 
}) 

然后在你的模板:

{{taxRateValue.content}} 
+0

我会用'PromiseObject'替代。 – locks

0

交际与店里面应该发生的路线,其中一个可以做

setupController(controller, model) { 
    var promise = this.get('store').findRecord('tax-rate', { current: true }) 
     .then(taxRate => taxRate.get('taxRateValue')) 
     .then(taxRateValue => controller.set('taxRateValue', taxRateValue)); //pass actual value 

    controller.set('taxRateValue', promise); //pass promise to template to display loader 
    }