自动对象到数组转换

时间:2012-11-19 06:58:01

标签: javascript ember.js handlebars.js ember-data

我在客户端使用Ember-Data&服务器端的ruby从postgres SQL数据库中获取数据,

当我从服务器端执行App.Person.findAll(App.Person)时,我将返回一个ruby哈希对象数组

现在当我console.log(typeof App.Person.findAll(App.Person))时,它会打印'object'

但是当我在把手中使用它时,

//Here I set in my ArrayController this.set('content', App.Person.findAll(App.Person));
{{#collection contentBinding="content"}} 
  Name: {{view.content.name}}
{{/collection}}

它打印所有名称,好像我们正在迭代一个对象数组,ember是否将对象转换为数组?

1 个答案:

答案 0 :(得分:2)

尝试使用 App.Person.findAll(App.Person).toArray() 查看记录数组,以及 record.toJSON()将记录视为对象;即。

App.Person.findAll(App.Person).map(function(record) { 
    return record.toJSON();
}

findAll()返回的结果是RecordArrayhttps://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/store.js#L378

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/record_arrays/record_array.js

RecordArray扩展了ArrayProxy,继承了toArray()方法。