我有一个代表活动的模型。活动有几个标准属性,如“start_time”、“type”、“sub_type”等,但是每种类型/子类型都有一组可以设置的属性,用户可能想要扩展这个集合所以本质上,有很多可能的属性。
我可以 - 至少现在 - 使用DS.attr(type)
命令显式设置标准属性,但希望将所有未定义的属性与DS.attr("string")
. 这可能吗?
这是一个简单的例子:
App.Activity = DS.Model.extend({
type: DS.attr("string"),
sub_type: DS.attr("string"),
name: DS.attr("string"),
description: DS.attr("string"),
is_private: DS.attr("boolean"),
start_time: DS.attr("datetime"),
end_time: DS.attr("datetime"),
icon: DS.attr("string")
});
传入的数据可能如下所示:
{
"id" : "1",
"type": "exercise",
"sub_type": "running",
"name": "Went Running",
"description" : "Went for a 7 mile run.",
"is_private" : false,
"start_time" : "2013-07-09 08:00:06",
"end_time" : "2013-07-09 09:05:00",
"icon": "run",
"distance" : 7,
"caloric_burn" : "950"
},
{
"id" : "2",
"type": "food",
"sub_type": "eating",
"name": "Ate Lunch",
"description": "Ate lunch at home.",
"is_private" : false,
"start_time" : "2013-07-09 12:15:08",
"end_time" : "2013-07-09 13:05:00",
"icon": "food",
"caloric_intake" : "825",
"qual_healthiness" : "5",
"qual_enjoyment" : "9"
},
如上所述,我希望像“caloric_burn”这样的非标准属性与字符串的默认类型相关联。在“下一个版本”中,我真正想要的是使用Marker API 端点查找“类型”,但我很乐意首先解决这个简化问题。
- - 更新 - -
Marker API 仅提供元/参考信息。因此,例如,您将从上述活动的标记旁加载中获得以下内容:
markers: [
{
"id": "distance"
"insight": "marker",
"typing": "number",
"uom_context": "length",
"min-value": 0
},
"id": "caloric_burn",
"insight": "marker",
"typing": "number",
"uom_context": "energy",
"min-value": 0
},
{
"id": "caloric_intake"
"insight": "marker",
"typing": "number",
"uom_context": "energy",
"min-value": 0
},
{
"id": "qual_healthiness",
"insight": "static-choice",
"choices": "healthyness-scale",
"typing": "number",
"min-value": 0,
"max-value": 10
}
]
问题是可能包含数百个(甚至数千个)不同的标记,我不想全部定义它们(那是在用户自定义开始发挥作用之前)。实际上,对于任何给定的事务,实际上只有几个标记需要添加到 Activity 的静态定义中。我希望能够在对象的序列化过程中识别模型中不匹配的属性,然后动态添加它们。