迭代 ember 模型查询 [英] Iterate over an ember model query

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

问题描述

this.store.findAll('game').then(function(results){
   // RUN SOME OPERATION ON THEM
})

我想知道如何使用 results 变量.我明白我可以做到

I would like to know how I can play with the results variable. I understand I can do

results.get('firstObject') // returns the first object.

我想知道我可以用它做什么.results 集合是否有任何 api 文档?

I'd like to know everything else I can do with it. Is there any api documentation for the results collection?

谢谢!

推荐答案

来自 余烬指南

下面的方法,将返回 Promise,将其解析为 Record 或 RecordArray.

The below methods, will return the Promise, it will be resolved to Record or RecordArray.

store.findAll() 返回一个满足 DS.RecordArrayDS.PromiseArray.
store.findRecord 返回一个承诺,该承诺将通过 record 解决.
store.query() 以与 findAll 相同的方式返回一个 DS.PromiseArray.

store.findAll() returns a DS.PromiseArray that fulfills to a DS.RecordArray.
store.findRecord returns a promise that will be resolved with the record.
store.query() returns a DS.PromiseArray in the same way as findAll.

下面两个是同步方法,它将检索商店中可用的内容并返回记录本身.它不会请求服务器获取数据.

The below two are synchronus method, it will retrieve what is available in the store and returns record itself. it will not request the server to fetch data.

store.peekAll 直接返回一个 DS.RecordArray.
store.peekRecord 直接返回记录

store.peekAll directly returns a DS.RecordArray.
store.peekRecord direclty returns record

需要注意的是 DS.RecordArray 不是 JavaScript数组,它是一个实现 Ember.Enumerable 的对象.这是重要 因为,例如,如果您想通过以下方式检索记录索引,[] 符号将不起作用——你必须使用objectAt(index) 代替.

It's important to note that DS.RecordArray is not a JavaScript array, it's an object that implements Ember.Enumerable. This is important because, for example, if you want to retrieve records by index, the [] notation will not work--you'll have to use objectAt(index) instead.

Ember.Enumerable,大部分时间我碰巧使用关注,
forEach 进行迭代
map 转换为新的数组
filterBy findBy 用于基于单个属性检查的过滤
toArray 转换为普通的原生数组

From Ember.Enumerable, most of the time I happened to use the following,
forEach to iterate
map to transform to new Array
filterBy findBy for filtering based on single property check
toArray converting to normal native array

这篇关于迭代 ember 模型查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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