开发者

Backbone.js - Access referrer in controller function

开发者 https://www.devze.com 2023-03-06 06:04 出处:网络
I have a list of products (index view) and when a user clicks on a product, a Lightbox opens (show view). Now, when the user closes the lightbox, I want to redirect him back to the page he came from.

I have a list of products (index view) and when a user clicks on a product, a Lightbox opens (show view). Now, when the user closes the lightbox, I want to redirect him back to the page he came from. The problem is that the Lightbox might have been opened from several different places. It's best demons开发者_JAVA技巧trated with an example:

Product "Foo" appears on 3 different pages:

/                  (the home page)
/tag/bar           (all products tagged with "bar")
/tag/baz/page-3    (all products tagged with "baz", paginated)

Now I have to decide where to redirect the user when the Lightbox closes. At this stage, there is no information available about which page the Lightbox was opened from.

So I figured I save a "returnTo" path when opening the Lightbox (rendering the show view). In the controller function, which is triggered by the hash change and renders the show view, I can't seem to get to any "referrer" information, though.

Is there any way to see what page triggered the hash change? Or is there a better way to solve this problems?

All suggestions are appreciated! :)

EDIT:

I solved the problem by setting a cookie. The cookie is set anytime a index site (might be any of the three mentioned above) is visited. Then, when the Lightbox is closed, the user is redirected to the cookie value, which is the last opened index site. It would be great, though, if there was a way built in to Backbone.js to "remember" pathes somehow. Julien mentioned in one of the comments that it might be possible to add such functionality.


You can trigger the browser back button within JS. Assuming you set everything right, the browser should go back to the previous set hash and voila.

0

精彩评论

暂无评论...
验证码 换一张
取 消