javascript 如何检索 Ember.js 模型的所有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15658442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:34:18  来源:igfitidea点击:

How to retrieve all properties of an Ember.js model

javascriptember.js

提问by joscas

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?

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

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

回答by piotrze

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

您可以简单地在模型上使用toJSON方法并从对象中获取键。

Ember.keys(model.toJSON())

Note that will not return you keys for relations.

请注意,不会返回您的关系键。

回答by Michael

You can also use this:
http://emberjs.com/api/data/classes/DS.Model.html#property_attributeshttp://emberjs.com/api/data/classes/DS.Model.html#method_eachAttribute

你也可以使用这个:
http: //emberjs.com/api/data/classes/DS.Model.html#property_attributes http://emberjs.com/api/data/classes/DS.Model.html#method_eachAttribute

Ember.get(App.User, 'attributes').map(function(name) { return name; });
Ember.get(userInstance.constructor, 'attributes').map(function(name) { return name; });

There's also similar properties for relationships too.

关系也有类似的属性。

回答by calcsam

An easy way to print out the fields and their values:

打印字段及其值的简单方法:

Ember.keys(model.toJSON()).forEach(function(prop) { console.log(prop + " " + model.get(prop)); } )

回答by mavilein

There is no easy way, but you could try a custom mixin like this:

没有简单的方法,但您可以尝试像这样的自定义 mixin:

Ember.AllKeysMixin = Ember.Mixin.create({
    getKeys: function() {
        var v, ret = [];
        for (var key in this) {
            if (this.hasOwnProperty(key)) {
                v = this[key];
                if (v === 'toString') {
                    continue;
                } // ignore useless items
                if (Ember.typeOf(v) === 'function') {
                    continue;
                }
                ret.push(key);
            }
        }
        return ret
    }
});

You can use it like this:

你可以这样使用它:

App.YourObject = Ember.Object.extend(Ember.AllKeysMixin, {
 ... //your stuff
});
var yourObject = App.YourObject.create({
  foo : "fooValue";
});
var keys = yourObject.getKeys(); // should be ["foo"];

回答by mistahenry

in 2018: use Ember Data's eachAttribute.

2018 年:使用 Ember Data 的eachAttribute

So, given a model Foo:

所以,给定一个模型 Foo:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
    "name": attr('string'),
    "status": attr('string')
});

Let's get the model's definition via the constructor:

让我们通过构造函数获取模型的定义:

var record = this.store.createRecord('foo', {
    name: "model1",
    status: "status1"
});
this.modelClass = record.constructor;

and invoke a function for each of its Ember Data attributes:

并为其每个 Ember Data 属性调用一个函数:

modelClass.eachAttribute(function(key, meta){ 
   //meta provides useful information about the attribute type
}