Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a knockoutJS custom binding for simple jQuery plugin

Tags:

knockout.js

Is there a general approach or at least a set of steps a KnockoutJS developer needs to take to create a binding for simple jquery plugins.

For example, if the plugin is triggered like this in "common code":

$('#tag1').tagsInput({ // my parameters here });

how would a simplest custom KO binding for this plugin look like?

like image 409
Maxim V. Pavlov Avatar asked Nov 06 '12 17:11

Maxim V. Pavlov


1 Answers

Here is a common way, for example for jQuery button:

ko.bindingHandlers.jqButton = {
    init: function(element, valueAccessor) {
        var options = valueAccessor() || {};
        $(element).button(options);
    }
};

<button data-bind="click: greet, jqButton: { icons: { primary: 'ui-icon-gear' } }">Test</button>

Read this article for some best practices: http://www.knockmeout.net/2011/07/another-look-at-custom-bindings-for.html

like image 165
Artem Vyshniakov Avatar answered Nov 10 '22 09:11

Artem Vyshniakov