Backbone.js - 控制器功能中的访问引荐来源网址

发布于 2024-11-07 12:29:39 字数 719 浏览 4 评论 0原文

我有一个产品列表(索引视图),当用户单击产品时,会打开一个灯箱(显示视图)。现在,当用户关闭灯箱时,我想将他重定向回他来自的页面。问题是灯箱可能是从几个不同的地方打开的。最好用一个例子来演示:

产品“Foo”出现在 3 个不同的页面上:

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

现在我必须决定灯箱关闭时将用户重定向到哪里。在此阶段,没有关于灯箱是从哪个页面打开的信息。

所以我想在打开灯箱(渲染显示视图)时保存一个“returnTo”路径。 不过,在由哈希更改触发并呈现显示视图的控制器函数中,我似乎无法获取任何“引荐来源网址”信息。

有什么方法可以查看哪个页面触发了哈希更改? 或者有更好的方法来解决这个问题吗?

感谢所有建议! :)

编辑:

我通过设置 cookie 解决了这个问题。 每当访问索引站点(可能是上述三个站点中的任何一个)时都会设置 cookie。然后,当灯箱关闭时,用户将被重定向到 cookie 值,即最后打开的索引站点。 不过,如果 Backbone.js 中有一种内置方法可以以某种方式“记住”路径,那就太好了。 Julien 在其中一条评论中提到,也许可以添加这样的功能。

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 demonstrated 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-11-14 12:29:39

您可以在 JS 中触发浏览器后退按钮。假设您将所有内容设置正确,浏览器应该返回到之前设置的哈希值,瞧。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文