Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Long term it really might be best to bite the bullet and use the classic combination of the mapping plugin, a JSON representation of the top-level view model rendered into the page to initialize with, and templates for sub view models. But I understand your pain re existing MVC partial logic, so......</p> <p>Are you currently rendering a separate script block as part of each sub partial's view to create its Knockout view model and apply its bindings?</p> <p>Could you not get away with a single binding context and define some kind of module in the page which owns the top-level view model and has its child view models added to it incrementally as the sub partials render, then at the bottom of the page, on DOM ready, apply the bindings?</p> <p>So, each sub partial's script block would make a call into some function on your module passing in the sub model's raw JSON, which includes its Guid key. Your module then creates a new instance of the sub view model and sets the parent view model as a reference inside it, and pushes it into an observable array of sub view models on the parent view model. Then you have a reference each way.</p> <p>I'm not sure that you can get around having some kind of page level manager for this structure (if that's what you meant by not polluting the global namespace). In any case, isn't it good to have these partial view models all owned by a single object rather than floating in the global namespace? (if they are)</p> <p>Hope I understand your context correctly.</p> <p>UPDATE: Your partials would still be loosely coupled in a sense. The binding expressions in the markup would obviously have to relate to an instance of a sub view model, but that could either be a data context chained down through a foreach on your todo list items or whatever, or explicitly set using the "with" expression, e.g. with someOtherModule.randomStandaloneSubViewModel</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload