javascript - Extend observableArray with data from a second source -


i create observablearray ajax source working pretty well. want extend of array items data second source.

for example:

{ id: 1, name: 'hugo', age: 18 } 

... later want add:

{ id: 1, city: 'new york', country: 'us' } 

... should result in:

{ id: 1, name: 'hugo', age: 18, city: 'new york', country: 'us' } 

is possible ko.mapping plugin? did tests result mapped properties of items in array have been replaced properties second source.


solution

well, pretty easy solve. when adding new data mapping plugin have check key. existing data extended additional data.

ko.mapping.fromjs(modifications, {     key: function(data) {         return ko.unwrap(data.id);     } }, originaldata); 

if properties aren't observables can use ko.utils.extend.

var original = { id: 1, name: 'hugo', age: 18 }; var modifications ={ id: 1, city: 'new york', country: 'us' };   var result2 =  ko.utils.extend(original,  modifications);  console.log(json.stringify(result2)); 

see fiddle


Comments