如何检索Ember.js模型的所有属性 [英] How to retrieve all properties of an Ember.js model

查看:95
本文介绍了如何检索Ember.js模型的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ember.js中的表单,我想检索所有模型属性的列表,以便我可以在不同的时刻拍摄表单的状态。有没有办法获取模型的所有属性的列表?



例如,如果我的模型是:

  App.User = DS.Model.extend({
name:DS.attr('string'),
email:DS.attr('string '),
current_password:DS.attr('string'),
密码:DS.attr('string'),
password_confirmation:DS.attr('string'),
admin:DS.attr('boolean'),
}

然后我会喜欢这样的东西:

 > getEmberProps('User')

[name ,email,current_password,password,password_confirmation,admin]


解决方案

您可以在模型上使用至JSON 方法从对象获取密钥。




Ember.keys(model.toJSON())



请注意,不会返回您的关键字为relatio ns。


I'm working with forms in Ember.js and I want to retrieve a list of all model properties so that I can take snapshots of the state of the form at different moments. Is there a way to get a list of all properties of a model?

For example, if my model is:

App.User = DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  current_password: DS.attr('string'),
  password: DS.attr('string'),
  password_confirmation: DS.attr('string'),
  admin: DS.attr('boolean'),
}

Then I would like to have something like this:

> getEmberProps('User')

["name", "email", "current_password", "password", "password_confirmation", "admin"]

解决方案

You can simply use toJSON method on model and get the keys from object.

Ember.keys(model.toJSON())

Note that will not return you keys for relations.

这篇关于如何检索Ember.js模型的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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