获取任何属性在属性数据模型中的属性类型
问题描述:
使用模型的特定实例,是否有任何方法来获取任何给定属性的类型?例如,假设我有一个名为Person的模型。在模板中,我将此模型的实例和属性名称传递给辅助函数。在那个函数中,我希望能够找出是什么类型的属性。获取任何属性在属性数据模型中的属性类型
我见过的最接近的事情是这样的,直接从灰烬文档:
App.Person = DS.Model.extend({
firstName: attr('string'),
lastName: attr('string'),
birthday: attr('date')
});
var attributes = Ember.get(App.Person, 'attributes')
attributes.forEach(function(name, meta) {
console.log(name, meta);
});
// prints:
// firstName {type: "string", isAttribute: true, options: Object, parentType: function, name: "firstName"}
// lastName {type: "string", isAttribute: true, options: Object, parentType: function, name: "lastName"}
// birthday {type: "date", isAttribute: true, options: Object, parentType: function, name: "birthday"}
这将工作,预计在帮助我的方法,我不知道型号。我需要能够做这样的事情,并使其返回相同的信息:
Ember.get(person, 'attributes');
当然,我愿意做更多的事情是这样的:
person.getMetaInfoFor(property);
但是,这只是一厢情愿的想法在此刻。我只是想弄清楚一些未知模型的某些未知属性是字符串还是日期。任何帮助将不胜感激。
答
灰烬2.4 +,使用3210:
var attributeType
person.eachAttribute(function(name, meta){
if (name === property){
attributeType = meta.type
}
})
答
您可以通过使用JQuery类型()函数
function getMetaInfoFor(input){
if(jQuery.type(input) === "boolean"){
return boolean;
}
if(jQuery.type(input) === "number"{
return number;
}
if(jQuery.type(input) === "date"){
return date;
}
};
你需要写一个函数来您转换输入日期自定义格式按您的要求得到一个属性的类型。
你可以不断添加各类按本link
这将工作,除了,例如日期,其存储为日期时间字符串。 – 2015-02-12 15:43:29