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

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

问题描述

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

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"]

推荐答案

你可以简单地使用 toJSON 方法并从对象中获取键.

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天全站免登陆