Skip to content

Durandal Compose convention

Anders Malmgren edited this page Jan 27, 2014 · 3 revisions

Knockout.BindingConventions works out of the box with durandal, but compose bindings does not. This snippet will enable the library to create Compose bindings.

(function(ko) {
    isViewOrModel = function(value) {
        return value.indexOf("viewmodels/") === 0 || value.indexOf("views/") === 0;
    };

    delete ko.bindingConventions.conventionBinders.template;
    ko.bindingConventions.conventionBinders.compose = {
        rules: [function(name, element, bindings, unwrapped, type) { return (type === "object" || (type === "string" && isViewOrModel(unwrapped))) && !ko.bindingConventions.utils.nodeHasContent(element); }],
        apply: function(name, element, bindings, unwrapped, type, dataFn) {
            bindings.compose = dataFn;
        }
    };

    ko.bindingConventions.conventionBinders.text.rules.push(function(name, element, bindings, unwrapped, type) {
        return !isViewOrModel(unwrapped);
    });
})(ko);

It will delete template convention and add a Compose convention. If you supply a object reference it will use that as a ViewModel. There are limitations that you need to known about when using string with the compose convention, if you want to supply it with a model or view-url string the path most begin with "viewmodels/" or "views/". Otherwise the library cant tell if its a compose string or a normal string that should use the text convention.

Clone this wiki locally