Ember.js 将动态模型设置为默认值

Ember.js 将动态模型设置为默认值,ember.js,Ember.js,我有一个表示活动的模型。活动具有多个标准属性,如“开始时间”、“类型”、“子类型”等。但是,每种类型/子类型都有一组可以设置的属性,用户可能希望扩展这组属性,因此本质上可能有很多属性 我可以使用DS.attr(type)命令显式地设置标准属性,但我希望所有未定义的属性都与DS.attr(“字符串”)关联。这可能吗 下面是一个简单的例子: App.Activity = DS.Model.extend({ type: DS.attr("string"), sub_type: DS

我有一个表示活动的模型。活动具有多个标准属性,如“开始时间”、“类型”、“子类型”等。但是,每种类型/子类型都有一组可以设置的属性,用户可能希望扩展这组属性,因此本质上可能有很多属性

我可以使用
DS.attr(type)
命令显式地设置标准属性,但我希望所有未定义的属性都与
DS.attr(“字符串”)关联。这可能吗

下面是一个简单的例子:

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"
},
如上所述,我希望像“卡路里燃烧”这样的非标准属性与默认类型的字符串相关联。在“下一个版本”中,我真正想要的是使用标记API端点查找“类型”,但我很乐意首先解决这个简化问题

----更新----

标记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
          }
 ]

问题是可能有数百(甚至数千)个不同的标记可以潜在地,我不想全部定义它们(那是在用户自定义开始之前)。实际上,对于任何给定的事务,实际上只有少数标记需要添加到活动的静态定义中。我希望能够在序列化对象的过程中识别哪些属性在模型中不匹配,然后动态添加它们

必须在余烬数据中声明模型的属性。没有这一点,它就无法进行肮脏的检查

您可以将
RESTAdapter
扩展到类似这样的内容,但这将是一个新的突破

在适配器中,当记录加载完成时,可以根据相应的模型及其声明的属性检查属性。如果它发现一个未声明的属性,则使用
App.Activity.reopen
,重新打开该类,添加该属性并重复其他属性

但是,如果可能的话,回到api,找出一种方法来将所有属性描述为另一个模型,比如通过与活动的多对多关系来描述
ActivityAttribute

编辑:已在上展开ActivityAttribute。 而不是具有许多次要属性的活动模型,如
开始时间
结束时间
卡路里燃烧
,这些属性可能是可选的

活动有许多标记。 标记属于活动

带有侧加载的对应json为

{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "marker_ids": [
      1, 2, 3, 4
    ]
  ],
  "markers": [
    { "id": 1, "name": "start_time"          , "value ": "foo" },
    { "id": 2, "name": "end_time"            , "value ": "foo" },
    { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
    { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
  ]
}
{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "markers": [
      { "id": 1, "name": "start_time"          , "value ": "foo" },
      { "id": 2, "name": "end_time"            , "value ": "foo" },
      { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
      { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
    ]
  ]
}
或者,如果有嵌入式记录

{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "marker_ids": [
      1, 2, 3, 4
    ]
  ],
  "markers": [
    { "id": 1, "name": "start_time"          , "value ": "foo" },
    { "id": 2, "name": "end_time"            , "value ": "foo" },
    { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
    { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
  ]
}
{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "markers": [
      { "id": 1, "name": "start_time"          , "value ": "foo" },
      { "id": 2, "name": "end_time"            , "value ": "foo" },
      { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
      { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
    ]
  ]
}

您必须在ember数据中声明模型的属性。没有这一点,它就无法进行肮脏的检查

您可以将
RESTAdapter
扩展到类似这样的内容,但这将是一个新的突破

在适配器中,当记录加载完成时,可以根据相应的模型及其声明的属性检查属性。如果它发现一个未声明的属性,则使用
App.Activity.reopen
,重新打开该类,添加该属性并重复其他属性

但是,如果可能的话,回到api,找出一种方法来将所有属性描述为另一个模型,比如通过与活动的多对多关系来描述
ActivityAttribute

编辑:已在上展开ActivityAttribute。 而不是具有许多次要属性的活动模型,如
开始时间
结束时间
卡路里燃烧
,这些属性可能是可选的

活动有许多标记。 标记属于活动

带有侧加载的对应json为

{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "marker_ids": [
      1, 2, 3, 4
    ]
  ],
  "markers": [
    { "id": 1, "name": "start_time"          , "value ": "foo" },
    { "id": 2, "name": "end_time"            , "value ": "foo" },
    { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
    { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
  ]
}
{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "markers": [
      { "id": 1, "name": "start_time"          , "value ": "foo" },
      { "id": 2, "name": "end_time"            , "value ": "foo" },
      { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
      { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
    ]
  ]
}
或者,如果有嵌入式记录

{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "marker_ids": [
      1, 2, 3, 4
    ]
  ],
  "markers": [
    { "id": 1, "name": "start_time"          , "value ": "foo" },
    { "id": 2, "name": "end_time"            , "value ": "foo" },
    { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
    { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
  ]
}
{
  "activities": [
    "id": 1,
    "type": "exercise",
    // other basic properties

    "markers": [
      { "id": 1, "name": "start_time"          , "value ": "foo" },
      { "id": 2, "name": "end_time"            , "value ": "foo" },
      { "id": 3, "name": "caloric_burn"        , "value ": "foo" },
      { "id": 4, "name": "food_caloric_intake" , "value ": "foo" },
    ]
  ]
}

json中是否有任何属性告诉您额外的字段?
type
sub_type
会在这方面发挥作用吗?如果是,我将为公共字段创建一个基础模型,然后为每个场景创建扩展基础模型的模型(只要不是超大范围的可能性),然后使用适配器和序列化器为每个负载(或部分负载)使用每个模型类型@MilkyWayJoe这是一个很好的建议,但不幸的是类型/子类型只会更强烈地“建议”属性。虽然有一种动态的方法可以识别类型信息,但它是相当动态的,即通过调用标记API来识别类型信息,但这需要动态更改模型。json中是否有任何属性可以告诉您额外的字段?
type
sub_type
会在这方面发挥作用吗?如果是,我将为公共字段创建一个基础模型,然后为每个场景创建扩展基础模型的模型(只要不是超大范围的可能性),然后使用适配器和序列化器为每个负载(或部分负载)使用每个模型类型@MilkyWayJoe这是一个很好的建议,但不幸的是类型/子类型只会更强烈地“建议”属性。它是相当动态的,虽然有一种动态的方式来识别类型信息,这将是通过调用Marker API来实现的,但这需要动态更改模型。也许我可以通过使用
registerTransform
将“details”属性定义为类型“object”来摆脱它。详细信息属性将是所有非标准属性的名称-值字典。。。这是黑客行为还是合理的?尽管我更倾向于支持你的
App.Activity。重新打开
建议。我该如何连接到记录加载中,以便执行重新打开?虽然如果我更好地理解您的多对多方法,我更愿意这样做。通过API有一个很好的“元模型”,它允许