2

I have a pair viewmodel/view in my application that it is used for displaying different data. The data that is displayed is generated by some user input and a separate route for each set of data is registered on the router.

The problem is that because the same view/viewmodel is used for different routes, when navigating between them, the binding between view-viewmodel is not refreshed.

Hooking up to canReuseForRoute callback I managed to get the viewModel refresh, but the view is not refreshed.

I tried to use cacheVews on the router binding but the views were still not refreshed.

How can I make the router/routing composition refresh when navigating to a different route that is using the same view/viewmodel?

EDIT : (more info) Hooking on the binding callback I discovered that Durandal binds the view only the first time the module is used. If I try to navigate to a different route with the same module (view/viewmodel) it does not call the binding callback again.

Razvan
  • 3,017
  • 1
  • 26
  • 35
  • Are your viewmodels instances or singletons? Are you reloading the data each time you visit a new route? –  Mar 19 '14 at 12:04
  • The viewmodels are singletons but for a workaround I use `canReuseForRoute` which tells durandal to reactivate the module therefore the data loads properly. I can't find how to re-bind the view to the new data. – Razvan Mar 19 '14 at 12:20
  • Is there any reason you wouldn't want instances, especially if you are, in fact, reloading data? –  Mar 19 '14 at 13:57
  • Is there any reason why I would? As I mentioned, viewmodel data **is reloaded**. **The view** (HTML) is the one that is not. I managed to get it working if I implement `canReuseForRoute` both on the viewmodel and on the shell module. I still don't like this solution though, because it refreshes all the other composed views(navbar, sidebar, etc.). – Razvan Mar 19 '14 at 15:08
  • Yes, you would go with instance-based modules precisely to accomplish what it is you're trying to accomplish, without the side effects. Not to mention the memory management benefits. Almost all of our modules have both an instance part and a singleton part. The singleton part offers services to the rest of the application, and the instance part is the module proper. –  Mar 19 '14 at 15:29

1 Answers1

2

Try overriding areSameItem on the activator as explained in this answer. Returning false should get Durandal to go through the whole page lifecycle again, whereas I think using canReuseForRoute doesn't necessarily.

Community
  • 1
  • 1
gerrod
  • 6,119
  • 6
  • 33
  • 45
  • That do it. Apparently they have implemented the method in a very basic way so it cannot detect if the viewmodel has different data. – Razvan Mar 20 '14 at 08:29