Question

i have SPA in durandal using requireJS. My approach is that on logout from my application, i want all html+js to be cleaned, cause for example: when logging with specific user some controls are disabled, and on logging with other user, those controls supposed to be enabled. It seems that the files are cached. I wish them to be cached when moving between pages in that app by the user, but on logout and login again - to start everything from begining.

thanks

Was it helpful?

Solution

This smells like you are going about it the wrong way.

Apparently, you build your views on the server end, perform logic there that disables/enables controls etcetera. Now you indeed run into problems, because if the HTML is cached, so is the logic that was used to build that HTML.

You can hack your way through this by 'clearing the cache' on logout. But what if in the future, a user's rights can be changed while he is still logged on? You have to invent yet another hack to make this work. Ultimately, you'll create a ton of spaghetti code that is very brittle.

Instead, the views, IF you want to cache them, should NOT contain any logic. The logic that enables or disables elements, and indeed everything else, should be dictated by your JavaScript and the data you receive from the backend. A view is just a HTML template that consumes the data that's in the JavaScript viewmodel.

Basically, you need to make a switch in your mind and start using Durandal/Knockout the way it's supposed to be used: MVVM, bindings in your HTML that set the state of the DOM based on data in your viewmodel. What you're doing right now is 'hard-coding' the state of the HTML when you render the HTML on the server, and you're trying to hack your way through the non-dynamic nature of this solution.

If you do want to pursue your current line of coding: the straightforward way of achieving a full reset of your JS and HTML is to do a full page refresh, thus 'leaving your SPA'.

OTHER TIPS

using urlArgs you can disable caching, add this to your require.config:

urlArgs: "_disable_cache_=" +  (new Date()).getTime()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top