javascript - When will be the extender function will get called -


i newbie knockout js. want implement dynamic validations 1 observable. want use extender function. not calling. have created jsfiddle. doubt when called.

the code is

// here's data model var viewmodel = function(first, last) { this.firstname = ko.observable(first).extend({logchange: "sri" }); this.lastname = ko.observable(last);  this.fullname = ko.computed(function() {     // knockout tracks dependencies automatically. knows fullname depends on firstname , lastname, because these called when evaluating fullname.     return this.firstname() + " " + this.lastname(); }, this);  ko.extenders.logchange = function(target, option) {     alert("log change function")     target.subscribe(function(newvalue) {         alert("subscribe function:  "+option + ": " + newvalue);     }); return target; }; };  ko.applybindings(new viewmodel("hello", "world")); // makes knockout work 

regards, srinivas

although explicitly not stated in documentation

any custom extender definition has come before first time use it.

so move ko.extenders.logchange part outside of viewmodel function:

ko.extenders.logchange = function(target, option) {         alert("log change function")     target.subscribe(function(newvalue) {         alert("subscribe function:  "+option + ": " + newvalue);     });     return target; };  var viewmodel = function(first, last) {     this.firstname = ko.observable(first).extend({logchange: "sri" });     this.lastname = ko.observable(last);      this.fullname = ko.computed(function() {         // knockout tracks dependencies automatically. knows fullname         // depends on firstname , lastname, because these called when         // evaluating fullname.         return this.firstname() + " " + this.lastname();     }, this); }; 

demo jsfiddle.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -