knockout.js - Knockout custom binding handlers - multiple arguments and function callbacks best practice? -


i created binding handler incorporate jquery validation plugin form using data-bind syntax. found myself needing supply more 1 piece of info handler. needed supply flag enforce validation , callback fire once validation passed.

questions:

  1. what best practice supplying multiple args? relied on object notation syntax, supply binding , check binding via "allbindings" param passed handler...

  2. what best practice supplying callback function handler?

below js code defining handler , html code apply handler:

     <form id="step1"          data-bind="jqvalidation:{enforce: true,                                   submithandler: dosomethinginvm}">            <fieldset data-bind="with:searchrequest">             //fields            </fieldset>            <button type="submit">submit</button>      </form> 

     ko.bindinghandlers.jqvalidation = {          update: function (element, valueaccessor, allbindingsaccessor, viewmodel) {             var accessor = valueaccessor();             //need unwrapobservable??             if (accessor.enforce) {                 $(element).find(':submit').removeclass('cancel');                 $(element).validate({                     submithandler: function () {                         if ($.isfunction(accessor.submithandler))                             accessor.submithandler();                     }                 });             } else                 $(element).find(':submit').addclass('cancel');         }     }; 

your approach here follows pattern used ko think valid. granted use allbindingsaccessor. way have interpreted usage of method

  1. share properties between multiple bindings, example bubbleevent binding used indicate other binding deals events not bubble them.
  2. to allow complex binding handlers aware of other bindings , adjust behavior.

the best practice passing handlers have them named members on view model rather inline on binding.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -