我在客户端使用 Ember-Data,在服务器端使用 ruby 从 postgres SQL 数据库获取数据,
当我从服务器端执行 App.Person.findAll(App.Person)
时,我将返回一个 ruby 哈希对象数组
现在,当我执行 console.log(typeof App.Person.findAll(App.Person))
时,它会打印 'object'
但是当我在 Handlebars 中使用它时,
//Here I set in my ArrayController this.set('content', App.Person.findAll(App.Person));
{{#collection contentBinding="content"}}
Name: {{view.content.name}}
{{/collection}}
它会打印所有名称,就好像我们正在迭代对象数组一样,ember 是否将对象转换为数组?
最佳答案
尝试使用
App.Person.findAll(App.Person).toArray()
查看记录数组,以及
record.toJSON()
将记录视为对象;即
App.Person.findAll(App.Person).map(function(record) {
return record.toJSON();
}
findAll() 返回的结果是一个 RecordArray
:
https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/store.js#L378
RecordArray
扩展了 ArrayProxy
,它继承了 toArray()
方法。
关于javascript - 自动对象到数组的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449021/