允许模型包含对象数组 [英] Allowing models to contain array of objects

查看:98
本文介绍了允许模型包含对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能有一个包含对象数组的模型,而不会将这些对象分离为具有唯一的ID?

Is it possible to have a model that contains an array of objects, without separating out those objects into having unique IDs?

例如,我有一个幻灯片,可能从API返回的多个参数:

For example, I have a slide with multiple parameters that may come back from the API:

slide: {
    "id": 1,
    "name": "stack-overflow-page",
    "type": "webpage"
    "parameters": [
        {"key": "url", "value": "http://stackoverflow.com"},
        {"key": "extension", "value": "/questions/ask"}
    ]
}

在我的幻灯片配置页面上,我想配置幻灯片及其参数列表。由于参数没有明确的ID(复合ID - SlideID,Key),因此分离这些参数并对每个参数进行API调用似乎是奇怪的。

On my slide configuration page, I want to configure the slide and it's list of parameters. Since the parameters don't have explicit IDs (Composite ID - SlideID, Key), it would seem odd to separate these and make API calls for each parameter.

我配置和使用这样的模型?

How can I configure and work with a model such as this?

推荐答案

最简单的方法是简单地做

The most straightforward approach is to simply do

parameters: DS.attr()

您可以定义您真正想要的自己的数组转换,但只有当您需要/想要专门的行为时。如果您更改阵列元素内的某些内容,您将不得不自行处理污垢,您可以使用

You could define your own array transform it you really want to, but only if you need/want specialized behavior. If you change something inside an element of the array, you will have to handle dirtification on your own, which you can do with

model
  .set('parameters.firstObject.key', 'www.google.com')
  .notifyPropertyChange('parameters');

这篇关于允许模型包含对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆