Javascript 首选客户端路由解决方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6315505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:14:35  来源:igfitidea点击:

Preferred client side routing solution?

javascriptjquerybackbone.jsurl-routingknockout.js

提问by lorefnon

I am designing a one page browser based web application.

我正在设计一个基于网页浏览器的 Web 应用程序。

JQueryis already being used in my application. I am currently planning to use KnockoutJSfor data-binding and UI management. However I have used Backbone.jsin past, and I have been quite impressed with the routing facilities provided by the controller layer.

JQuery已在我的应用程序中使用。我目前计划KnockoutJS用于数据绑定和 UI 管理。但是我Backbone.js过去使用过,控制器层提供的路由功能给我留下了深刻的印象。

I have also looked into some JQuery-based solution like pathjsfor client side routing.

我还研究了一些基于 JQuery 的解决方案,例如用于客户端路由的pathjs

It would be great if someone could offer suggestions and guidelines for choosing a proper routing system.

如果有人可以为选择合适的路由系统提供建议和指导,那就太好了。

I am not willing to use Backbone.jsinstead of KnockoutJSbecause of the excellent data-binding facilities of KnockoutJS.

我不愿意使用Backbone.js代替,KnockoutJS因为KnockoutJS.

采纳答案by lorefnon

After going through a bit of exploration I have found the solution. @Eisenhauer Backbone's routing facilities are awesome, but what if I am not using Backbone. As I outlined in my question, I am already using KnockoutJS that provides me a client side MVVM model and so its not really a good idea to put an additional MVC implementation in the same page.

经过一番探索,我找到了解决方案。@Eisenhauer Backbone 的路由功能很棒,但如果我不使用 Backbone 呢?正如我在我的问题中概述的那样,我已经在使用 KnockoutJS,它为我提供了一个客户端 MVVM 模型,因此在同一页面中放置一个额外的 MVC 实现并不是一个好主意。

For people looking for a standalone routing solution, pathjs is a simple and elegant solution.

对于寻找独立路由解决方案的人来说,pathjs 是一个简单而优雅的解决方案。

A more flexible, powerful and standalone routing solution is Crossroads.js available at http://millermedeiros.github.com/crossroads.js/. It is very nicely documented and really powerful,so can be adapted to any set of requirements.

一个更灵活、强大和独立的路由解决方案是 Crossroads.js,可从http://millermedeiros.github.com/crossroads.js/ 获得。它有很好的文档记录并且非常强大,因此可以适应任何一组要求。

回答by Spycho

You could use the History API rather than a hashbang approach?

您可以使用 History API 而不是 hashbang 方法吗?

  • It allows you to manipulate the URL displayed to the user.
  • It means that back / forward work sensibly.
  • Bookmarking the page works sensibly.
  • It's better in terms of performance, because if the user refreshes a page (or hits a link to the page from an external source) the correct content is loaded first time rather than having to load a blank page and then grab the correct content via AJAX.
  • 它允许您操作显示给用户的 URL。
  • 这意味着后退/前进工作明智。
  • 为页面添加书签很有效。
  • 它在性能方面更好,因为如果用户刷新页面(或从外部源点击页面链接),则第一次加载正确的内容,而不必加载空白页面,然后通过 AJAX 获取正确的内容.

The downside is that older browsers will not support it. You could fall back on a hashbang approach (if you really have to). I don't know of any existing frameworks that do this though. I would prefer to fall back on not dynamically loading content (i.e. full page reloads when the user clicks a link). The history API will become more and more widely supported, so this "problem" will reduce over time.

缺点是旧浏览器不支持它。您可以使用 hashbang 方法(如果您真的必须这样做)。我不知道有任何现有的框架可以做到这一点。我宁愿依靠不动态加载内容(即当用户单击链接时重新加载整页)。历史 API 将得到越来越广泛的支持,所以这个“问题”会随着时间的推移而减少。

Here's a working examplewith some documentation. From the address bar, it looks like pages are loading "old school" (full page refresh) but if you take a look at the console (Firebug, Chrome dev tools), you can see that the content is being grabbed via an AJAX request.

这是一个带有一些文档的工作示例。从地址栏中,看起来页面正在加载“老派”(整页刷新),但如果您查看控制台(Firebug、Chrome 开发工具),您可以看到内容是通过 AJAX 请求获取的.

Mozilla has some docsabout it.

Mozilla 有一些关于它的文档

回答by Shekhar

Also there is excellent Director

还有优秀的导演

回答by Deepak Thomas

Crossroads.jsseems to be one of the best around. One major advantage of it being, it doesn't rely on window.location for routing. It's lightweight as it sticks to do just routing and routing alone.

Crossroads.js似乎是最好的之一。它的一个主要优点是,它不依赖 window.location 进行路由。它是轻量级的,因为它坚持只做路由和路由。

Others you may want to try: finch.js, davisjs(based on pushState so that you can use existing links as a fallback if JS fails)

其他你可能想尝试:finch.jsdavisjs(基于 pushState 这样你就可以在 JS 失败时使用现有链接作为后备)

Appending an update:March 30 2015 - I had moved to AngularJS late last year. Hands-down the best switch form JQ, has a lot of modules like routing in-built.

附加更新:2015 年 3 月 30 日 - 去年年底我已转移到 AngularJS。最好的交换机形式 JQ,内置了很多模块,如路由。