finnsson / pagerjs

The Single Page Application Framework for KnockoutJS
http://pagerjs.com/
MIT License
272 stars 68 forks source link

Is is possible to use same viewModel for multiple views? #226

Open mrshawn191 opened 6 years ago

mrshawn191 commented 6 years ago
<div data-bind="page: {id: 'xxx', title: 'xxx : User Settings', sourceOnShow: 'view/test.html', with: vm, afterShow: _.bind(vm.readDataById, vm) }"></div>

<div data-bind="page: {id: 'yyy', title: 'yyy : User Settings', sourceOnShow: 'view/test2.html', with: vm, afterShow: _.bind(vm.readDataById, vm) }"></div>

Im getting Uncaught ReferenceError: Unable to process binding "text: function (){return username }"

Problem only occurs when navigating from one page to another. During initial load both pages works ok.

mw44118 commented 6 years ago

First off, yes, I use the same viewmodel with multiple pages all the time.

Second, I don't think you need to use _.bind(vm.readDataById, vm); I think you can probably just do

afterShow: vm.readDataById

and that method will fire every time somebody hits that page.

The error you are getting, UncaughtReferenceError, is likely because inside your template HTML files, you're referencing some observable that is not in scope in that block. For example, maybe you're in a foreach loop, and the username observable lives on the parent, not on each element you are looping through.

Are you sure that a username observable lives on your vm object?