我一直在使用ember数据,但我从来没有遇到过检查模型中是否有关键存在的情况。通常,我检查模型中的密钥是否存在值,如
if(model.get('keyname')) {
console.log('value present')
}但是现在我想检查一下模型对象中是否存在密钥。任何帮助都将不胜感激。
忘了在这里提到-这里的模型是嵌入式记录,我在我的项目中没有一个嵌入式记录的模型文件(我是为嵌入式记录自动生成模型)。因此,我无法使用属性或字段。
发布于 2017-03-27 22:57:28
比如说app/model/Person.js
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
firstName: attr('string'),
relations: hasMany('person'),
});属性
一个映射,其键是模型的属性(由DS.attr描述的属性),其值是该属性的元对象。
import Ember from 'ember';
import Person from 'app/models/person';
let attributes = Ember.get(Person, 'attributes')
attributes.forEach(function(meta, name) {
console.log(name, meta);
});输出:
firstName {type: "string", isAttribute: true, options: Object, parentType: function, name: "firstName"}字段
一个映射,其键是模型的字段,其值是描述字段类型的字符串。模型的字段是其所有属性和关系的统一体。
import Ember from 'ember';
import Person from 'app/models/person';
let attributes = Ember.get(Person, 'fields')
attributes.forEach(function(meta, name) {
console.log(name, meta);
});产出:
lastName attribute
users hasManyhttps://stackoverflow.com/questions/43056839
复制相似问题