Manipulating a RecordArray

女生的网名这么多〃 提交于 2020-01-01 03:49:06

问题


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

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

  • paginating through the set of records within
  • adding records from another findQuery into the array

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.


回答1:


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.



来源:https://stackoverflow.com/questions/15102668/manipulating-a-recordarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!