javascript - Unable to modify ko observable when function defined with object prototype -


i understand in general bad practice modify prototypes object.prototype.whatever syntax when using knockout, i'm trying understand why isn't working @ all:

var foo = function() {         var self = this;         self.bar = ko.observable("bar");     };     foo.prototype.capitalizer = function() {         self.bar("bar");     };     var vm = function() {          var self = this;         self.whatever = new foo();     }; 

js fiddle here: http://jsfiddle.net/vvdo7z70/8/

when works expected:

var foo = function() {         var self = this;         self.bar = ko.observable("bar");         self.capitalizer = function() {             self.bar("bar");         }     };     var vm = function() {          var self = this;         self.whatever = new foo();     }; 

js fiddle here: http://jsfiddle.net/vvdo7z70/10/

is not possible pass relevant ko bindings object.prototype syntax? or there way it?

for one, self isn't defined in foo.prototype.capitalizer. once that's fixed, need note binding click: whatever.capitalizer using function, not method, say, whatever context not provided. instead, vm provided context. work:

foo.prototype.capitalizer = function () {     this.whatever.bar("bar"); }; 

or

click: whatever.capitalizer.bind(whatever) 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -