0

Absolutely love Durandal's architecture/clarity. But have ran into trouble getting durandal 2.0.1 to recognize any custom knockout binding.

My example View contains:

<div data-bind="test: {}" />

The binding is defined within an appBindings.js that contains:

define([ 'jquery', 'knockout'], 
  function ($, ko) {
    ko.bindingHandlers.test = {
      init: function(element, valueAccessor, allBindingsAccessor) {alert(0);},
      update: function(element, valueAccessor, allBindingsAccessor) {alert(1);}
    }
    var model = function(){} 
    return new model();
});

That is required from main:

requirejs.config({
    paths: {
        ...
        'durandal':'../lib/durandal/js',
        'jquery': '../lib/jquery/jquery-1.9.1',
        'knockout': '../lib/knockout/js/knockout-3.1.0',
        'bootstrap': '../lib/bootstrap/js/bootstrap',
        ...
        'bindings': 'bindings/appBindings'
    },
    shim: {
        'bootstrap': ['jquery'],
        //'ko-binding-test': ['jquery','knockout'],


    }
});

define(['libs/core','durandal/system', 'durandal/app',
            'durandal/viewLocator','services/resourceService','bindings'],  
    function (core, system, app, viewLocator, resourceService, bindings) {
    //...
});

Observations:

  • Chrome's traffic shows appBindings.js being loaded.
  • can breakpoint within appBindings.js as it gets loaded.
  • but no matter what I try, init/update are never invoked.
  • the binding is functional outside of durandal (http://jsfiddle.net/skysigal/8LJw5/3/)

I've also tried various other ways of loading the binding definition:

  • using in main a define + shim of a non-AMD file containing the binding definition.
  • loading a non-AMD file (containing the binding definition) directly from index.html using a script tag
  • making it a dependency of the view, not main.

Anyone have a suggestion as to the proper way to do this?

Thank you.

GôTô
  • 7,974
  • 3
  • 32
  • 43
Ciel
  • 591
  • 4
  • 12
  • So `appBindings.js` is loaded, but can you confirm it is loaded **after ko** and **before running `ko.applyBindings`**? – GôTô Jul 01 '14 at 12:05
  • Bonsoir! Yes. appBidings.js was loaded, after ko, and before ko.applyBindings. ... The issue turned out to be caused by a binding on the page that failed earlier. I just didn't look for that, thinking it just had to be the custom binding... – Ciel Jul 01 '14 at 12:19
  • Happens! Nice French skills :) – GôTô Jul 01 '14 at 12:21

0 Answers0