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
Post a Comment