javascript - Automapping ajax data during ajax put or post in knockout -
during ajax request, can use ko.mapping.fromjs data server , automapping. also, can use ko.mapping.tojs post or put ajax data server in knockout.
however, assumption every value in ko.mapping.tojs pass in ajax call. use delete or ignore remove property should not pass server.
recently, have came problem follow. how pass data want without explicitly assign or ignore data 1 one cumbersome. thinking restructuring view model may job not know how start.
function myviewmodel() { var self = this; // these data should not pass in ajax call self.data1 = ko.observable(); self.data2 = ko.observable(); self.data3 = ko.observable(); ... self.data50 = ko.observable(); // these data should not pass in ajax call self.nodata1 = ko.observable(); ... self.nodata10 = ko.observable(); // these should not pass in ajax call self.function1 = function() { } self.function2 = function() { } self.function3 = function() { $.ajax({ .. type: 'post', data: { ko.mapping.tojs(self) }, success: {} } ... self.function50 = function() {} }; ko.applybindings(new myviewmodel());
well there technique , call grouping required set without disturbing 2 way binding , other dependencies .
viewmodel:
var viewmodel = function (first, last, age) { this.firstname = ko.observable(first); this.include = { // set can pass in ajax call & skip remaining . lastname: ko.observable(last), age: ko.observable(age) } }; ko.applybindings(new viewmodel("planet", "earth", 25));
working sample here preview showing intact .
ps: imho not wise way alter/rebuild our viewmodel (in complex applications maintainability can @ stake)
Comments
Post a Comment