如何列出模型中定义的所有属性?
例如,如果我们有一些假想的博客应用程序的变体:
App.Post = DS.Model.extend({
title: DS.attr('string'),
text: DS.attr('string'),
comments: DS.hasMany('App.Comment')
});
Run Code Online (Sandbox Code Playgroud)
然后,我正在寻找在没有App.Post模型实例的情况下迭代属性的可能性:
# imaginary function
listAttributes(App.Post)
Run Code Online (Sandbox Code Playgroud)
这样的函数可以生成一个提供模型属性名称和类型的数组:
[{
attribute: "title",
type: "string"
},
{
attribute: "text",
type: "string"
}]
Run Code Online (Sandbox Code Playgroud)
如何用Ember实现这一目标?
截至2016年11月(Ember v2.9.0),解决此问题的最佳方法是使用eachAttribute迭代器.
API参考= http://emberjs.com/api/data/classes/DS.Model.html#method_eachAttribute
modelObj.eachAttribute((name, meta) => {
console.log('key =' + name);
console.log('value =' + modelObj.get(name));
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2068 次 |
| 最近记录: |