KnockOutJS – How to do Multiple ViewModels in a single View ?

KnockOutJS – How to do Multiple ViewModels in a single View ?

Asked on November 14, 2018 in knockout js.
Add Comment


  • 3 Answer(s)

      For view on same page, one simple way to do this is to have a master view model have an array  of the different view models.

    masterVM =
    

    {   vmA : new VmA(),   vmB : new VmB(),   vmC : new VmC(), }

      The masterVM can have other properties if needed, for the page itself. Communication between the view models would not be difficult in this situation as it is good to relay through the masterVM, or  use the $parent / $root in bindings, or some other custom options.

    Answered on November 14, 2018.
    Add Comment

    Use the ko.applyBindings() method for this:

    ko.applyBindings(myViewModel, document.getElementById('someElementId'))
    

    It prevent the activation to the element with ID someElementId and its descendants.

    Answered on November 14, 2018.
    Add Comment

        For single view,Check the MultiModels plugin for Knockout JShttps://github.com/sergun/Knockout-MultiModels

    Answered on November 14, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.