Ko.mapping creation function, object extension

Is it possible to change the scheme (due to the lack of a better term) of the object during the matching process? I would suggest that this is so, I just can't get it to work. I am trying something like this:

var data = { itemOne: 'someData', itemTwo: 'moreData' } var mapping = { "newItem": { create: function(options) { return ko.observable(false); } } }; ko.mapping.fromJS(data, mapping, _model.observableArrayPart); 
+6
source share
1 answer

Here is an example showing how to set up your array and the key that defines it so that you can apply updates using the mapping plugin: http://jsfiddle.net/rniemeyer/LHeQZ/

 var data = [ { id: 1, first: "Bob", last: "Smith" }, { id: 2, first: "Jim", last: "Jones" }, { id: 3, first: "Delete", last: "Me" } ]; var updatedData = [ { id: 1, first: "Robert", last: "Smith" }, { id: 2, first: "James", last: "Jones" }, { id: 4, first: "New", last: "Guy" } ]; var Person = function(data) { this.id = data.id; this.first = ko.observable(data.first); this.last = ko.observable(data.last); this.full = ko.computed(function() { return this.first() + " " + this.last(); }, this); }; var dataMappingOptions = { key: function(data) { return data.id; }, create: function(options) { return new Person(options.data); } }; var viewModel = { people: ko.mapping.fromJS([]), loadInitialData: function() { ko.mapping.fromJS(data, dataMappingOptions, viewModel.people); }, loadUpdatedData: function() { ko.mapping.fromJS(updatedData, dataMappingOptions, viewModel.people); } }; ko.applyBindings(viewModel); 
+14
source

Source: https://habr.com/ru/post/911158/


All Articles