Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember-Data: Knowing when a RecordArray is completely populated

I have a situation where isLoaded on a DS.RecordArray changes to true but the content, length property of the RecordArray is still empty, 0 at that 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 while the second is 0 and the template which uses this data is empty. When I see the actual AJAX request I see that the request does return an array of records. And the length and content of the RecordArray do change some time later as seen in the Browser console by doing:

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

However this code(below) does populate the content in the template, but it runs 12 times...

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

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

).observes('followRequests.length')

What's the right way to know when the RecordArray is completely populated...??

like image 960
Shakeeb Ahmed Avatar asked Nov 03 '22 06:11

Shakeeb Ahmed


1 Answers

Since the time Ember.js uses promises you can do this

App.FollowRequests.find().then(function () {
    // This callback will fire when array is loaded
});
like image 126
Myslik Avatar answered Nov 09 '22 21:11

Myslik