Search code examples
durandaldurandal-2.0durandal-navigation

Durandal router cacheViews not working (v2.1.0)


I am trying to figure out how to use the Durandal v2.1.0 router to navigate between views using the router.navigate(); function. I have been able to navigate back and forth between two views but each time the view is recreated. I want to be able to cache these views so if the router routes to a view that has already been activated once it will simply get the existing view from the cache (along with it's current viewmodel state such as observable values, etc...).

I though that I could just use the cacheViews: true option in my router binding in the shell but this doesn't seem to be working. Any help would be greatly appreciated.


Solution

  • So... it turns out that caching is working by default and I misunderstood how the caching system works. In my case, I had been executing all of the view initialization functionality inside the activate function of the viewmodel. However, this activate function is executed each time you route to a new view thus my initialization functionality was being re-run each time the view was navigated to. A simple solution was to just put an ifActivated flag inside the activate function and only run the initialization functionality if this was the first time that the view had been activated.