操纵RecordArray [英] Manipulating a RecordArray

查看:122
本文介绍了操纵RecordArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从App.ModelName.find()返回的RecordArray。



我想用它做一些事情,如:




  • 通过

  • 中的记录进行分页,将另一个findQuery添加到数组中



我可能会感到困惑,但似乎很难(或至少没有记录)如何处理从find( )/ findAll()/ findQuery(),而不是遍历集合并正常显示它们。



这是从所有()返回的数组进一步复杂化, ,似乎更接近一个身份图,也许。



这些都不可能,但如果我不能打开问题并开始工作

解决方案

Ember Data返回的 RecordArray 真的意味着修改。特别是, Model.find()(sans-argument)和 Model.all()返回保持的活动数组随着新的匹配记录可用更新。



如果要操作一系列模型,最好使用 Model.find({})(该参数使它使用 findQuery())并观察 isLoaded 属性。这样的东西:

 查询:null,

init:function(){
//应该在路由中真的这样做
this.set('query',Model.find({}));
},

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

现在内容返回一个普通的旧数组您可以使用它(尽管您仍然需要等待记录加载才能开始修改数组)。



如果问题是您想要一个查询来继续更新,然后考虑使用 Model.filter(),它返回一个活动数组,如 find(),但接受一个匹配的功能。请注意,令人困惑的是,但非常有意的是,没有一个 find() all() filter()有一个 isLoaded 属性。



至于分页,你可以尝试一个简单的mixin方法,或者更精细的基于轨道的解决方案


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.

解决方案

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.

这篇关于操纵RecordArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆