Hot Towel SPA Durandal Knockout and Dynatree

2019-08-03 18:55发布

问题:

I am trying to get the dynatree control to work in the Hottowel viewmodel, based on this fiddle, http://jsfiddle.net/EZgNs

var ViewModel = function() {
    var self = this;
    self.initialized = ko.observable(false);
    self.items = ko.observableArray();

    // Use JSFiddle echo to simulate an AJAX service
    (function() {
        $.ajax({ url:"/echo/json/", data:data, type:"POST",
                 success:function(data)
                 {
                   // Map the returned JSON to the View Model  
                   ko.mapping.fromJS(data, {}, self.items);
                   self.initialized(true);
                 }
               });
     })();    
};

ko.bindingHandlers.dynatree = {
    update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        setTimeout( function() { $(element).dynatree()}, 0);
    }
};

ko.applyBindings(new ViewModel());

Having trouble understanding how to edit the vm to incorporate the dynatree ko.

回答1:

First off, you shouldn't be binding your viewmodel explicitly; let Durandal do this for you. Next, you can register any knockout binding handlers at the outset by placing the binding handler in either the main.js or the shell.js file's activate method. This way, you guarantee that the binding handler is available to all viewmodel/view combinations at binding time.