1

I'm building a Durandal SPA that may benefit from a cacheViews setting of true or false, depending on the individuals usage of the app. This is how I currently have it set in shell.html:

<div class="page-host" data-bind="router: {  cacheViews:true }"></div>

And in a child router like in samples/index.html:

<div>
    <!--ko router: {  cacheViews:false }--><!--/ko-->
</div>

1) Can these be changed at runtime? If so, how?. Does it matter that one is using data-bind and the other is using ko comment style?

2) How granular is this value? Is it per "router" or per "route"? As you can see, I have a parent router and a child router, so there are 2 places in my html code where I can set cacheViews. From my testing, it appears as if they are independent of each other. Can anyone confirm? Can I set this value on individual routes like /#page1, /#page2, /#samples/list, etc?

3) Because the page event life-cycle is different between true/false I need to have some specific logic in my vm depending on this value. How can I retrieve it for the current route?

Thanks

mwill
  • 424
  • 7
  • 21

1 Answers1

1

1) Can these be changed at runtime? If so, how?

With any knockout binding, the binding is recomputed if the bound value triggers subscriptions. That means cacheViews can be changed at runtime if it is an observable.

I'm not sure if the right way to do this is ko.observable({ cacheViews: false }) or { cacheViews: ko.observable(false) }. In fact, I believe which one will work is somewhat dependent on the version of knockout you're running, but one of them will work.

2) Does it matter that one is using data-bind and the other is using ko comment style?

No.

3) How granular is this value? Is it per "router" or per "route"?

Per router. More specifically, per binding.

4) Can I set this value on individual routes like /#page1, /#page2, /#samples/list, etc?

There's nothing out of the box that lets you set cacheViews per route. cacheViews is processed in the compose binding, which is being called under the hood in the router and so you would need to hook in there. However, Durandal is excellent about exposing hooks at every part of the lifecycle for custom logic. I'm sure with a little digging you can come up with your own customization to handle this.

5) Because the page event life-cycle is different between true/false I need to have some specific logic in my vm depending on this value. How can I retrieve it for the current route?

You would want to create the observable from (1) in your viewModel and have your associated view read it, like any other observable in knockout. Then, you will be able to access the current value directly off the viewModel. For example:

viewModel

viewModel = {
    settings: ko.observable({
        cacheViews: false
    })
}

view

<!-- ko router: settings -->
Matthew James Davis
  • 12,134
  • 7
  • 61
  • 90
  • don't forget to upvote and accept if you've found this helpful – Matthew James Davis Dec 17 '14 at 16:30
  • Very good idea setting it in the vm and using an observable. I couldn't get it to work exactly as your code. I made the settings variable an observable : var settings = ko.observable( { cacheViews: true } ); and then to update at run time var setCacheViews = function (val) { settings({ cacheViews:val }) }; – mwill Dec 17 '14 at 19:18
  • haha i wasn't sure which would do it, i knew it would be one of them though. i'll edit my code. – Matthew James Davis Dec 17 '14 at 19:26