Knockout.js - Multiple ViewModels per page; page-wide functions with different model contexts -


i building huge page multiple forms on user fill out in sequence.

i want make each section own model , planning on having masterviewmodel imports submodels.

each section, however, has edit & save buttons have same functions:

  1. edit toggles model edit mode
  2. save validates inputs, saves data (via ajax), , toggle state of model back

the difference between sets of buttons model context.

i'm having trouble making page-level save & edit function can reference different models using masterviewmodel/subviewmodels.

does have guidance on this?

thanks.

if had function on root view model, can call anywhere click: $root.save.

when knockout calls function set context (this) current data , pass first argument. so, first argument contain current model , can process there.

here sample: http://jsfiddle.net/rniemeyer/v22gd/

var viewmodel = {     one: {         name: ko.observable("bob")        },     two: {          name: ko.observable("sue")     },     save: function(model) {         alert(ko.tojson(model));     } };  ko.applybindings(viewmodel); 

with markup like:

<div data-bind="with: one">     <input data-bind="value: name" />     <button data-bind="click: $root.save">save</button> </div>  <div data-bind="with: two">     <input data-bind="value: name" />     <button data-bind="click: $root.save">save</button> </div> 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -