Allowing models to contain array of objects

穿精又带淫゛_ 提交于 2019-12-23 04:28:16

问题


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

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"}
    ]
}

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?


回答1:


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');


来源:https://stackoverflow.com/questions/25389963/allowing-models-to-contain-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!