问题
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