javascript - How to extend a mapped knockout viewmodel "on the fly" with additional property? -


i'm getting json object array asp.net web api , map knockout viewmodel (ko.observablearray). while binding model document "unable process binding" error.

this because json dto api doesen't have editable property defined. how can add on fly while mapping? should false default.

here's code:

var accessory = function (id, leftonpatient, reject, amount, patchnumber, additionalinfo, editable) {   var self = this;    self.id = ko.observable(id);   self.leftonpatient = ko.observable(leftonpatient);   self.reject = ko.observable(reject);   self.amount = ko.observable(amount);   self.patchnumber = ko.observable(patchnumber);   self.additionalinfo = ko.observable(additionalinfo);   self.editable = ko.observable(editable);  }  function medicinesandaccessoriesviewmodel() {   var self = this;    self.accessories = ko.observablearray([]);   self.error = ko.observable();    function getallaccessories() {     ajaxhelper(accessoriesuri, 'get').done(function (data) {       ko.mapping.fromjs(data, {}, self.accessories);          });   };    getallaccessories();  };  var vm = new medicinesandaccessoriesviewmodel(); ko.applybindings(vm, document.getelementbyid("medicinesandaccessoriestab")); 

html:

<ol class="list-group list_of_items padding-top-15" data-bind="foreach: accessories">         <li class="list-group-item">             <div class="container-fluid">                 <div class="col-md-4">                     <span data-bind="text: id, visible: !editable()"></span> //here error .... ... .. . 

so, how can define editable(false) property client side each accessory loaded api? every other property dto loaded api. can done somehow while mapping?

you're not using accessory constructor function anywhere @ all, word occurs once @ definition in snippet...

you need utilize mapping definition , tell map part of json correctly, e.g.:

function getallaccessories() {     ajaxhelper(accessoriesuri, 'get').done(function (data) {         var mappingrules = {             'accessories': {                 create: function(options) {                     return new accessory(                         options.data.id,                          options.data.leftonpatient,                          options.data.reject,                          options.data.amount,                          options.data.patchnumber,                          options.data.additionalinfo,                          options.data.editable);                 }             }         };          ko.mapping.fromjs(data, mappingrules, self.accessories);          }); }; 

this should make sure editable observable there. try force boolean !! in case:

self.editable = ko.observable(!!editable);  

ps. recommend making constructor function less complicated reducing number of arguments 1 (e.g. data or dto), return new accessory(options.data) in above snippet.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -