EventContext.redirect and browser back button

831 views
Skip to first unread message

Paul Selden

unread,
Apr 26, 2011, 7:41:43 PM4/26/11
to Sammy.js
I've encountered a functional difference between Sammy and how a
regular page loads when using the EventContext.redirect function.

Let's say I have this route:

$.get('#/page1', function(context){
context.redirect('#/page2');
});

When I browse to #/page1, it will redirect correct to #/page2, but
pressing back will take me back to #/page1 (which will redirect me to
#/page2 again). This is in contrast to how a normal browser redirect
works where the redirect is essentially transparent. Is there a way to
make it remove the page that was redirected from the history? I'm
using a custom LocationProxy that uses History.js to support both
HTML5 history and hash change page transitions... do I need to monkey
around in there to attempt to remove it?

Thanks,
Paul

Stephen Smyth

unread,
Apr 27, 2011, 3:03:05 PM4/27/11
to sam...@googlegroups.com
Hi Paul,
I agree, the Sammy redirect does seem broken--its behaves more like a forward--I've been meaning to propose a change for this. To get the correct behaviour, you do need to alter the HashLocationProxy, setLocation can be changed from:

    setLocation: function(new_location) {
      return (window.location = new_location);
    } 

to:

   setLocation: function(new_location) {
      return window.location.replace(new_location);
    }

However this would most probably have unwanted side effects, so it may be wiser to create a new function replaceLocation and alter redirect to call this instead. Additionally I would create a counterpart to redirect called forward, which would function exactly as the current redirect does now.

Not a complete solution but a start...

.Steve


--
You received this message because you are subscribed to the Google Groups "Sammy.js" group.
To post to this group, send email to sam...@googlegroups.com.
To unsubscribe from this group, send email to sammyjs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sammyjs?hl=en.


Paul Selden

unread,
Apr 27, 2011, 5:37:26 PM4/27/11
to Sammy.js
Hi Stephen, thanks for your input.

Yes, I think it would be wise to propose something like
"replaceLocation" as an addition to the proxy interface.

Following your suggestions, I was able to come up with a working
solution (adding a function called replaceLocation to the HistoryProxy
and just manually calling it right before the redirect -- obviously a
more ideal solution would be to have it passed-through the app or
automatically called in redirect like you said... but didn't want to
monkey with Sammy internals too much).

var proxy = new HistoryLocationProxy(this);
this.setLocationProxy(proxy);

...

$.get('#/page1', function(context){
var page = "#/page2";
proxy.replaceLocation(page);
context.redirect(page);
});

Thanks for the help!

On Apr 27, 3:03 pm, Stephen Smyth <kaheg...@gmail.com> wrote:
> Hi Paul,
> I agree, the Sammy *redirect* does seem broken--its behaves more like a *
> forward--*I've been meaning to propose a change for this. To get the correct
> behaviour, you do need to alter the HashLocationProxy, *setLocation* can be
> changed from:
>
>     setLocation: function(new_location) {
>       return (window.location = new_location);
>     }
>
> to:
>
>    setLocation: function(new_location) {
>       return window.location.replace(new_location);
>     }
>
> However this would most probably have unwanted side effects, so it may be
> wiser to create a new function *replaceLocation* and alter *redirect* to
> call this instead. Additionally I would create a counterpart to *redirect*
>  called *forward,* which would function exactly as the current
> *redirect*does now.
>
> Not a complete solution but a start...
>
> .Steve
>

Aaron Quint

unread,
Apr 27, 2011, 7:44:47 PM4/27/11
to sam...@googlegroups.com
In case you werent aware - the next version of Sammy (0.7) is being tested right now and has seamless support for pushState/HTML 5 History.
You can find out more here:

--AQ

--------------------------------
Aaron Quint
http://www.quirkey.com

Paul Selden

unread,
Apr 28, 2011, 11:23:16 AM4/28/11
to Sammy.js
That's great news Aaron, and a very welcome addition. In the mean
time, I've created a proxy that essentially does the same thing (based
99% off the work at https://github.com/balupton/history.js):
https://gist.github.com/b4abb6d84ffb3a5728e0

If Sammy 0.7 ends up fufilling the same needs, then that's a whole lot
of code that I'll be able to retire!

Thanks for your hard work and great product!

-Paul

On Apr 27, 7:44 pm, Aaron Quint <aa...@quirkey.com> wrote:
> In case you werent aware - the next version of Sammy (0.7) is being tested right now and has seamless support for pushState/HTML 5 History.
> You can find out more here:http://groups.google.com/group/sammyjs/browse_thread/thread/de8e839cf...
>
> --AQ
>
> --------------------------------
> Aaron Quinthttp://www.quirkey.com

Vladimir Pavlikov

unread,
May 29, 2014, 6:06:49 AM5/29/14
to sam...@googlegroups.com
So I am looking at Sammy 0.7.4 and the problem is still there. Here's what I have:

// initialize the application
var app = Sammy('#main', function() {
// define a 'route'
this.get('#/', function(context) {
console.log('main');
  });

  this.get('#/page1', function(context) {
    console.log('at page 1');
    context.redirect('#/page2');
  });

  this.get('#/page2', function(context) {
    console.log('at page 2');
  });
});

// start the application
app.run('#/');

So when i press link

<a class="link" href="#/page1">go to page1</a>

I'm being redirected to page2, and I can't go back to, each back button press leads me to page2 again. Whats the workaround?

четверг, 28 апреля 2011 г., 19:23:16 UTC+4 пользователь Paul Selden написал:
Reply all
Reply to author
Forward
0 new messages