javascript - Clean and Rebind Data using KnockoutJs Template -


so bind knockout template follows:

first ajax, data pass data can call function named bindko:

function bindko(data) {             var length = data.length;             var insertrecord = {};              if (length > 0) {                 insertrecord = data[data.length - 1]; //last record empty premlimviewmodel insert                 insertrecord.add = true;                 data.splice(data.length - 1, 1); //remove blank insert record             }              function prelims(data) {                 var self = this;                 var model = ko.mapping.fromjs(data, { copy: ["_destroy"] }, self);                   self.bidpriceformatted = ko.computed({                     read: function () {                         var bidprice = this.bidprice();                         if (bidprice) {                             if (!isnan(bidprice)) {                                 var input = '<input type="text" value="' + bidprice + '"/>';                                 return $(input).currency({ decimals: 0 }).val();                             }                         }                     },                     write: function (value) {                         value = value.replace(/\d/g, '');                         this.bidprice(value);                     },                     owner:                 });                  return model;             }              var mapping = {                 create: function (options) {                     return new prelims(options.data);                 }             };              function viewmodel(prelimdata) {                 var self = this;                   self.prelims = ko.mapping.fromjs(prelimdata, mapping);                 self.remove = function (prelim) {                     self.prelims.destroy(prelim);                 };                 self.addoption = function () {                     var clone = jquery.extend(true, {}, insertrecord);                      self.prelims.push(ko.mapping.fromjs(clone));                 };             }              viewmodel = new viewmodel(data);             ko.applybindings(viewmodel);         } 

i have template defined can add , remove records, , user that:

<script type="text/html" id="prelimstemplate"> <!--template goodness--> </script> 

then, ajax call, records updated in datanbase, latest results returned , do:

ko.mapping.fromjs(newestdata, viewmodel) 

but not work because viewmodel complex. rebind template entirely. make disappear , reappear latest data.

wrap template in container can hook onto jquery.

when need trash use ko.cleannode , jquery .empty()

emptytemplate: function(){   ko.cleannode($('#template-container')[0]);   $('#template-container').empty(); } 

load template up

filltemplate: function(){     $('#template-container').html('<div data-bind="template: {name:\'templateid\', data: $data}"></div>');      ko.applybindings(data,$('#template-container')[0]) }, 

see my fiddle


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -