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:
- edit toggles model edit mode
- 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
Post a Comment