SteveSanderson/knockout.mapping — Issue #35: Differenciate between create/update and createItem/updateItem
Author: bikeshedder
Created: 2011-10-18 12:28:41+00:00
State: open

All in all I would prefer if create/update would not behave different for arrays than for normal scalars:

create: receives the entire source data/array as options.data
update: receives the target data/array as target and the source data/array as options.data

Since most use cases will probably want to perform a simple create/update per item, two extra methods could be introduced:

createItem: behaves the same as the current create function for arrays
updateItem: behaves the same as the current update function for arrays

When overwriting the normal update/create methods for arrays one must take care to implement the key logic, but it the end it would make it possible to perform extremely powerful mappings.

Example:

source: {"foo": {...fooProps...}, "bar": {...barProps...}}
target: [{"name": "foo", ...fooProps...}, {"name": "bar", ...barProps...}]

In an application I'm currently writing I wanted to do exactly this but had to change the server return value to the target array because I was not able to map objects to array properties of the target model. With the proposed change such mappings are possible and the API becomes much more logical.