Skip to content Skip to sidebar Skip to footer

Multiple Arrays In One Knockout.js Viewmodel

I have run into the case where I am having to use multiple ViewModels in the same view. I achieved that by using this kind of trick which allows multiple bindings on different item

Solution 1:

You've really 'melded' the two viewModels together... instead I would 'composite' them together, something like this:

var PermitsViewModel = function(permits) {
    // Permits functionality and observables here
}
var SupervisorsViewModel = function(supervisors) {
    // Supervisors functionality and observables here
}

var ProjectModel = function(supervisors, permits) {
    varself = this;
    self.supervisorsViewModel = new SupervisorsViewModel(supervisors);
    self.permitsViewModel = new PermitsViewModel(permits);

};

var viewModel = new ProjectModel(supervisorJSONArray,permitJSONArray);
ko.applyBindings(viewModel);

You could then use 'with' statements in the view to reference the appropriate view model.

Post a Comment for "Multiple Arrays In One Knockout.js Viewmodel"