wrschneider
wrschneider

Reputation: 18780

pagerjs - sharing view model with enclosing pages

If I have something like this with nested pages

<div data-bind="page: {id: 'top', withOnShow: topVM}">
  <div data-bind="page: {id: 'sub1', withOnShow: sub1VM}">
    <span data-bind="text:sub1Property"></span>
  </div>
</div>


topVM = function(callback) { callback({topProperty: XXX}); }

sub1VM = function(callback) { 
   // how can I access topVM here?
   callback({sub1Property: XXX}); 
}

How can I access the outer page's view model from the inner page's view model?

Upvotes: 1

Views: 249

Answers (1)

segFault
segFault

Reputation: 1228

<span data-bind="text:$root.topVMProperty"></span>

The link here explains the knockout contexts

http://knockoutjs.com/documentation/binding-context.html

According to your example you could also do this:

<span data-bind="text:$parent[1].topVMProperty"></span>

Upvotes: 3

Related Questions