0

I have a Rails app that I'm using with Backbone JS as a FE framework with Coffeescript syntax. Now to get to the point quickly I'm using a Backbone trick than replaces Rails's redirect_to method:

redirectTo = (something) ->
  Backbone.history.navigate('')
  Bacbkone.history.navigate("##{something}", {trigger: true})

And whenever I need a full refresh to my page for example when I'm creating something I use it(this is maybe a wrong approach but I'm new to Backbone). Now I also use localStorage["something"] to store values that I'll use for later. I also have a function:

$(window).on 'hashchange', ->
    window.localStorage.clear()

Here is the strange part. For example I redirect to a #products page with redirectTo('products') by some logic the localStorage needs to be cleared and I should not be able to use his values in the Router. But the funny part is I can cause it first goes to the action '#products' from the Backbone Router and then triggers the $(window).on('hashchange'). Any clues how can this happen and if this is something regular?

Vlatko Ristovski
  • 111
  • 1
  • 11

1 Answers1

0

Couple of points. 1. To make a complete refresh of page you can do

window.location.reload();

2. $(window).on 'hashchange' handler will fire whenever the hash part of URL on page changes.

What could be going wrong ?: I see already you are clearing localstorage on hashChange, but this hashChange handler will fire after the route has made transition to products page.

So whats the solution : ? Quick way is to clear the localstorage when you do redirectTo('products');

for example

//some code 

redirectTo('products');
window.localStorage.clear();

//Some more code

By this way you will never have localStorage cleared.

Hope that helps. Thanks

Kiba
  • 10,155
  • 6
  • 27
  • 31