2013-01-22 27 views
5

我有一種情況,在DS.RecordArray上的isLoaded更改爲true,但RecordArray的content,length屬性仍爲空,此時爲0,並且稍後纔會更改。Ember-Data:知道RecordArray何時完全填充

示例代碼(CoffeeScript的):

@set('followRequests', App.FollowRequests.find()) 

... 

whenDataLoads: (-> 

console.log @get('followRequests.isLoaded') 
console.log @get('followRequests.length') 
@set('content', @get('followRequests').toArray()) 

).observes('followRequests.isLoaded') 

第一個日誌聲明是真實的,而第二個是0和使用這種數據的模板是空的。當我看到實際的AJAX請求時,我發現請求確實返回了一組記錄。而RecordArray的長度和內容也發生變化一段時間後,如出現在瀏覽器控制檯做:

App.Router.myController.get('followRequests').get('length') ---> 12

但是這個代碼(如下)不填充內容的模板,但它運行12次......

whenDataLoads: (-> 
console.log @get('followRequests.isLoaded') 
console.log @get('followRequests.length') 

@set('content', @get('followRequests').toArray()) 

).observes('followRequests.length') 

什麼,知道什麼時候該RecordArray完全填充了正確的方式...?

+3

我相信這是燼數據中的錯誤,我打開[問題](https://github.com/emberjs/data/issues/652)的bug跟蹤系統這一點。很高興找到解決方法。 –

+0

@AdrienCoquio,謝謝,很高興知道別人也面臨這個問題。將跟蹤此,我真的沒有看到任何可能的解決方法.. –

回答

2

由於Ember.js使用諾言的時候,你可以做到這一點

App.FollowRequests.find().then(function() { 
    // This callback will fire when array is loaded 
}); 
0

在餘燼數據測試版1.0.0,你會使用路線或控制器所提供的store財產請求結果。

// fetch one 
var promiseArray = this.store.find('follow_request', follow_request_id); 

// fetch all 
var promiseArray = this.store.find('follow_request'); 

// set callbacks on promise array 
promiseArray.then(onFollowRequestSuccess, onFollowRequestFailure); 

// or set callbacks on promise object 
var promise = promiseArray.get('promise'); 
promise.then(onFollowRequestSuccess, onFollowRequestFailure); 

// or use computed property 
App.FollowRequestsController = Ember.ArrayController.extend({ 
    loadedCount: function() { 
     return this.get('[email protected]').filterBy('isLoaded', true).length; 
    }.property('[email protected]').cacheable() 
});