javascript - Bind all methods to 'this' -
i have iife methods defined in it, public , not.
in lot of functions (including private ones) use 'this' keyword, make sure i'm in right context, need use .call(this)
method. there way bind functions inside iife correct context, without having use call()
?
defining var self=this
not option since messes knockout context.
ko.utils.extend(singleselectdropdownviewmodel.prototype, (function () { var strings = { all: l10n('all') }; //public functions var init = function (params) { this.selectedvalue = params.selectedvalue; this.title = ko.observable(); this.items = params.items; this.menuopen = ko.observable(false); //would avoid using call , use _settitle() _settitle.call(this); }, ... //private functions _settitle = function(){ if (ko.unwrap(this.selectedvalue)){ var = util.index(this.items(),ko.unwrap(this.selectedvalue),'id'); = > -1 ? : 0; this.title(this.items()[i].text); } else { this.title(this.items()[0].text); } }, _hidemenu = function(){ this.menuopen(false); }, ... return { init: init }; })());
i suggest stick using this
in functions methods of object, , using function parameters rest of time. if so, code becomes:
ko.utils.extend(singleselectdropdownviewmodel.prototype, (function () { var strings = { all: l10n('all') }; //public functions var init = function (params) { this.selectedvalue = params.selectedvalue; this.title = ko.observable(); this.items = params.items; this.menuopen = ko.observable(false); //would avoid using call , use _settitle() _settitle(this); }, ... //private functions _settitle = function (obj) { if (ko.unwrap(obj.selectedvalue)){ var = util.index(obj.items(),ko.unwrap(obj.selectedvalue),'id'); = > -1 ? : 0; obj.title(obj.items()[i].text); } else { obj.title(obj.items()[0].text); } }, _hidemenu = function(obj){ obj.menuopen(false); }, ... return { init: init }; })());
Comments
Post a Comment