pagerjs icon indicating copy to clipboard operation
pagerjs copied to clipboard

Is is possible to use same viewModel for multiple views?

Open mrshawn191 opened this issue 7 years ago • 1 comments

<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.

mrshawn191 avatar Jan 12 '18 12:01 mrshawn191

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?

mw44118 avatar Feb 08 '18 13:02 mw44118