ker*_*nic 0 javascript promise computed-values ember.js ember-data
我正在尝试从服务器获取REST请求返回的对象的简单计数,以便在Ember.js中的另一个控制器中使用
出于这个原因,我需要向服务器发出一个额外的请求.基本上这是我的代码,它几乎可以工作..但还不是.也许有人可以找出原因.
它返回一个PromiseArray,这就是我.then()用来访问属性的原因.
App.TestController = Ember.ObjectController.extend({
totalCount: function() {
return this.store.find('question', {test: this.get('id')}).then(function(items) {
var count = items.get('content').get('length');
console.log(count); // This actually logs correct values
return count;
})
}.property('question')
})
Run Code Online (Sandbox Code Playgroud)
它做了它想做的事情,我在console.log()中打印出正确的值,但是当我尝试{{totalCount}}在视图模板中使用时,我得到的[object Object]不是整数.
另外,我是否正确观察了该questions物业?如果值在适当的控制器中发生变化,值会更新吗?
谢谢
您看到的问题是因为您正在返回承诺,因为财产的价值和把手不会为您评估该承诺.您需要做的是创建一个单独的函数,观察question然后调用您的商店以更新totalCount属性.这将是这样的.
App.TestController = Ember.ObjectController.extend({
totalCount: 0,
totalCountUpdate: function() {
var that = this;
this.store.find('question', {test: this.get('id')}).then(function(items) {
var count = items.get('content').get('length');
console.log(count);
that.set('totalCount', count);
})
}.observes('question')
})
Run Code Online (Sandbox Code Playgroud)