Tony Pitale
Tony Pitale

Reputation: 1202

Manipulating a RecordArray

I have a RecordArray that has come back from a App.ModelName.find().

I'd like to do some things with it, like:

I may be confused, but it seems like it's difficult (or at least undocumented) on how to work with the records that come back from find()/findAll()/findQuery() other than looping over the set and displaying them as normal.

This is further complicated by the array that gets returned from all(), which seems to be closer to an identity map, maybe.

None of this may be possible, but if it isn't I can open issues and start to work on that myself.

Upvotes: 3

Views: 1425

Answers (1)

ahmacleod
ahmacleod

Reputation: 4310

The RecordArrays returned by Ember Data aren't really meant for modification. In particular, Model.find() (sans-argument) and Model.all() return live arrays that keep updating as new matching records are available.

If you want to manipulate an array of models, you're best off using Model.find({})(the argument makes it use findQuery()) and observing the isLoaded property. Something like this:

query: null,

init: function() {
  // should really do this in the route
  this.set('query', Model.find({}));
},

content: function() {
  var query = this.get('query');
  return query && query.get('isLoaded') ? query.toArray() : [];
}.property('query.isLoaded')

Now content returns a plain old array and you can have your way with it (though you still need to wait for the records to load before you can start modifying the array).

If the issue is that you want a query to keep updating, then consider using Model.filter(), which returns a live array like find(), but accepts a matching function. Note that confusingly, but quite intentionally, none of find(), all(), and filter() have an isLoaded property.

As for pagination, you could try a simple mixin approach, or a more elaborate rails-based solution.

Upvotes: 4

Related Questions