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:
what best practice supplying multiple args? relied on object notation syntax, supply binding , check binding via "allbindings" param passed handler...
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
- share properties between multiple bindings, example bubbleevent binding used indicate other binding deals events not bubble them.
- 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
Post a Comment