javascript - Knockout.js custom binding design -


i not understand logic behind following , hoping can me understand. cleaning web app , have found following lines of code. app mvc app using knockout.js. there several custom bindings setup following structure:

var originalbindinginit = ko.bindinghandlers.binding.init; var originalbindingupdate = ko.bindinghandlers.binding.update;  ko.bindinghandlers.binding = {     init: function (element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext) {         originalbindinginit(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext);          // init code here...      },      update: function (element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext) {         originalbindingupdate(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext);          // update code here...     } }; 

i not understand why init & update being set variables outside of binding , fired on first line of each section of binding? seem me creating loop doing this.

can shed light on this? if it's covered in knockout documentation, have missed , apologize.

consider this:

var = 1; var originala = a; = 2; 

this way every place used has new value , can still use old one. since in javascript function object can same it. keep old 1 , overwrite new functionality.

var functiona = function () {}; var originalfunctiona = functiona; functiona = function () {}; 

it doesn't create loop because no longer has same reference. old reference overwritten new function. perfect example in question. we've got library knockout method binding. extend can modify original source of library have each time library updated. more such modifications need harder maintaining be.

so write new binding function @ place of original one. nice, if don't want change , extend little bit have write done in first place. why write again when written? copy original function new reference:

var originalbindinginit = ko.bindinghandlers.binding.init; var originalbindingupdate = ko.bindinghandlers.binding.update; 

and call when need job:

var bindinginit = function () {     // sth befeore     originalbindinginit( somevariable );     // sth after } 

we can put our extension @ beginning variable preparations , call original function does:

var someroundedvariable = math.round( somevariable ); originalbindinginit( someroundedvariable ); 

in knockout js there way writing custom binding handler. , call original one.

ko.bindinghandlers.verifyvalue = {     init: function(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext) {         // sth verify value , call other binding:         ko.bindinghandlers.value.init(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext);     },     update: function(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext) {         // rest of binding handled default value binding. pass on!         ko.bindinghandlers.value.update(element, newvalueaccessor, allbindingsaccessor, viewmodel, bindingcontext);     } }; 

both have cons , pros. can't modify view , have access viewmodel, sure don't need original binding without extensions (or keeping dangerous) other time need modified binding in few places not of them , it's better create new binding.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -