javascript - Knockout - How to reset Dynamic observable array with new value -
i can't reset observable array new value using lazy loading technic. can clear can't reset, not allowing me add new dynamic value.
fiddle http://jsfiddle.net/kspxa8as/
js
var = 1; optionsprovider = function(self) { var self = self || {}; self.options = {}; self.get = function(name, initialvalue) { if (!self.options[name]) { console.log("called - " + name); self.options[name] = ko.observablearray([initialvalue]); var requestheader = ''; settimeout(function() { var aa = [{name: "plant 1" + i, selected: true}, {name: "palnt 2" + i, selected: false}]; self.options[name](aa); i++; }, 2000); } return self.options[name]; }; return self; }; viewmodel = function() { var self = this; var k = 1; var ob = new optionsprovider(self); self.plantselected = ob.get("name" + k, ''); self.fillnewselect = function() { self.plantselected.removeall(); self.plantselected().push(ob.get("name" + k, '')); k++; }; }; ko.applybindings(new viewmodel());
html
<select class="n_imported_country" data-bind="options: plantselected, optionstext :'name' " > </select> <div data-bind="click: function(){ $root.fillnewselect(); }">click fill new select value</div>
i newbie knockout, great welcome answers.
try trick here passing instance of observablearray function , our job done
viewmodel:
var viewmodel = function() { var self = this; var k = 1; var ob = new optionsprovider(self); self.plantselected = ko.observablearray(); //declare observablearray ob.get("name" + k, '',self.plantselected) // call function passing observablearray instance self.fillnewselect = function() { self.plantselected.removeall(); ob.get("name", ''+ k,self.plantselected) k++; }; }; ko.applybindings(new viewmodel());
function :
var = 1; optionsprovider = function(self) { var self = self || {}; self.options = {}; self.get = function(name, initialvalue,instance) { //pass observablearray instance here if (!self.options[name] || self.options[name]()) { var requestheader = ''; settimeout(function() { var aa = [{name: "plant 1" + i, selected: true}, {name: "palnt 2" + i, selected: false}]; instance(aa); // assign data instance },2000); i++; } return true; }; return self; };
working sample here
Comments
Post a Comment