knockout.js - How to access outer function observable in inner function in viewmodel? -
i having main function , sub function inside that, this:
var main= function () { var self = this; self.value1 = ko.observable(""); var data = self.value1(); self.revaluate = ko.computed(function(){ data = self.self.value1(); // overwriting }); function inner(i1,i2) { var self= ; self.id1=ko.observable(i1); self.id2=ko.observable(i2); self.value2 = ko.observable(""); self.visibility = ko.computed(function() { if (data == 1) {return true;} else {false;} }); } } ko.applybindings(new main());
i need value1
value inside inner
function should dynamic.
i tried storing in variable , accessing it, it's not gonna solve case there scenario self.visibility
won't fire @ , update dynamically.
onload updated value1
work, have button "goto next stage", , onclick of button updating status, i.e self.value1(2)
. @ point self.visibility
should update there change in value1
.
i tried show need in simple example, in reality there's more complex looping going on.
any clue great.
for computed update when self.value1 updates have use inside computed function
self.visibility = ko.computed(function(){ if( self.value1() == 1) {return true;} else { return false;} }); }
main reason why computed didn't work because using data calculate computed simple js variable, why updated onload, data never reevaluated, computed never got updated. need use ko.observable computed update properly!
one other note: don't see you've used variable inner function in computed. if case in real example can transfer computed main function.
edit: don't have working code, , don't know how using inner function can't create working example point problems. self.value1 have scope problems should able overcome this, try passing variable, use global variable... whichever works you
Comments
Post a Comment