Ember-Data: know when RecordArray is full

I have a situation where isLoaded on DS.RecordArray changes to true, but the content, length property for RecordArray is still empty, 0 at this time and only changes later.

Sample code (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')

The first log statement is true, and the second is 0, and the template that uses this data is empty. When I see the actual AJAX request, I see that the request returns an array of records. Both the length and contents of the RecordArray may change after some time, as shown on the browser console by doing the following:

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

However, this code (below) fills the content in the template, but it works 12 times ...

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

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

).observes('followRequests.length')

What is the right way to find out when RecordArray is full ... ??

+5
source share
2

, Ember.js promises,

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

Ember Data beta 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('content.@each').filterBy('isLoaded', true).length;
    }.property('content.@each.isLoaded').cacheable()
});
0

All Articles