Skip to content
AndersMalmgren edited this page Jan 18, 2013 · 28 revisions

Welcome to the Knockout.BindingConventions wiki!

Knockout.BindingConventions is a Convention Over Configuration library for KnockoutJS, many of the convention rules are borrowed from the excellent Caliburn.Micro framework for WPF.

The convention HTML5 attribute

The library uses its own attribute, data-name. The value of the attribute specifies a member on the current binding context.

<!-- firstName is a member on the ViewModel -->
<span data-name="firstName"></span>

Built in conventions:

Adding own conventions

Its possible to add your own convention by adding a object to ko.bindingConventions.conventionBinders

ko.bindingConventions.conventionBinders.inputEnable = {
    rules: [function(name, element, bindings, unwrapped, type) { return type === "boolean" && element.tagName === "INPUT"; }],
    apply: function(name, element, bindings, unwrapped, type, data) {
        bindings.enable = data;
    }
};

Rules is an array of functions that all must return true for the convention to apply. Apply is a function that applies the convention. In the example above the custom convention will be starved by the built in input convention because it will bind the boolean to the checked binding. What you can do is to add a rule to the input convention like

ko.bindingConventions.conventionBinders.input.rules.push(function(name, element, bindings, unwrapped, type) { type !== "boolean" });

This will change the input convention so it will no longer bind against booleans

http://jsfiddle.net/gSAjH/1/

Clone this wiki locally