简体   繁体   English

使用Backbone.js路由器进行路由而不是使用服务器端代码的原因

[英]Reasons to use Backbone.js Router for routing instead of using server-side code

When and why would I use Backbone.js Router for routing instead of routing via server-side code? 何时以及为什么我会使用Backbone.js路由器进行路由而不是通过服务器端代码进行路由? Could someone elaborate on that since it's my first time exposed to do routing on client-side. 有人可以详细说明,因为这是我第一次接触到客户端的路由。

You've presented a false dichotomy. 你提出了一个错误的二分法。 The reality is that there is probably never going to be a situation when you'll use Backbone's router in place of a server-side solution. 实际情况是,当您使用Backbone的路由器代替服务器端解决方案时,可能永远不会出现这种情况。 That said, there is certainly a growing trend toward using client-side routers in general (not specifically Backbone's) to create one-page apps—eg, Ember.js . 也就是说,使用客户端路由器(特别是Backbone)创建单页应用程序(例如Ember.js )的趋势肯定会越来越 Here are your options: 以下是您的选择:

Only server-side routes 只有服务器端路由

This is the classic approach that is a big component of frameworks like Rails. 这是经典方法,是Rails等框架的重要组成部分。 It is a mature strategy that draws very bright lines between your models, views, and controllers. 这是一种成熟的策略,可以在模型,视图和控制器之间绘制非常明亮的线条。 It's certainly not going away anytime soon, and for good reason: it's great if you're not developing a one-page app, which most people aren't. 它肯定不会很快消失,并且有充分的理由:如果你没有开发一个单页应用程序,这是很好的,大多数人都没有。

Only client-side routes 只有客户端路由

This is what things like Ember offer you. 这就像Ember为你提供的东西。 You can write all of your routes on the client-side, and then the client is responsible for updating state, throwing old objects away, etc. This requires a robust JavaScript implementation of models, views, and controllers to work properly. 您可以在客户端编写所有路由,然后客户端负责更新状态,抛弃旧对象等。这需要强大的JavaScript实现模型,视图和控制器才能正常工作。 Otherwise you're going to quickly end up with a pile of rotten spaghetti. 否则你很快就会得到一堆腐烂的意大利面条。 If you're going to do this, do not use Backbone . 如果您打算这样做, 请不要使用Backbone Backbone's router works best for simple things like state. Backbone的路由器最适合像state这样的简单事情。 There is simply no clean way to use vanilla Backbone to replace a server-side router. 没有干净的方法来使用vanilla Backbone来替换服务器端路由器。

Hybrid approach 混合方法

A hybrid approach is where Backbone's router will shine. 混合方法是Backbone的路由器将闪耀的地方。 You use server-side routes to serve the views/templates, and then you enhance them with Backbone's routes. 您使用服务器端路由来提供视图/模板,然后使用Backbone的路由增强它们。 Here's a few examples of that: 以下是一些例子:

  1. A user profile page that has an inline editor. 具有内联编辑器的用户配置文件页面。 The route might be: /users/me#mode=edit , where /users/me is a typical route served by the server, and #mode=edit is a Backbone route that changes the view to an 'edit' mode where the user can edit his profile info. 路径可能是: /users/me#mode=edit ,其中/users/me是服务器提供的典型路由, #mode=edit是一个Backbone路由,将视图更改为用户的“编辑”模式可以编辑他的个人资料信息
  2. A calendar that highlights a date. 突出显示日期的日历。 The route might be: /calendars/work#date=today . 路线可能是: /calendars/work#date=today Here's an example of something you just can't do with a server-side route: highlight a particular cell of the calendar (namely, today ). 以下是您无法通过服务器端路由执行操作的示例:突出显示日历的特定单元格(即today )。

Unless you're set on writing a one-page app, it's safe to say that you won't benefit too much from using a client-side router. 除非您已经开始编写一个单页应用程序,否则可以肯定地说,使用客户端路由器不会带来太多好处。 And even if you are writing a one-pager, you probably shouldn't look to Backbone to do it. 即使你正在写一个单页面,你也许不应该看Backbone这样做。

Benefits 优点

  • fast, normally you only need to load one portion of the page 快,通常你只需要加载页面的一部分
  • memorable, history is kept page and you can control what goes into history, what's not 令人难忘,历史保持页面,你可以控制历史,什么不是
  • organized, as you are building a client-side application, it's good to have all the necessary logic at client side, including important component like dispatcher (router) 组织起来,在构建客户端应用程序时,最好在客户端拥有所有必要的逻辑,包括调度程序(路由器)等重要组件
  • easy to do, implementaion is almost same as server side, you specify routes and handlers, then links in the anchor href attr. 容易做,实现几乎与服务器端相同,您指定路由和处理程序,然后在锚点href attr中链接。

exception case 例外情况

  • when you want to do form submission consisting files, it's easier to just use action url to handle multi-part data 当您想要进行包含文件的表单提交时,只需使用操作URL来处理多部分数据就更容易了

just my 2-cents 只是我的2美分

Edit: deleted " as server-side" in memorable line. 编辑:在令人难忘的行中删除“作为服务器端”。

It's entirely a matter of preference. 这完全是一个偏好问题。 It's basically another version of asking when to do an AJAX request rather than a full request. 它基本上是另一个版本,询问何时执行AJAX请求而不是完整请求。 You could use Backbone entirely for routing with a single page app and then just have the back-end represent a pure representation of the model through an API. 您可以完全使用Backbone进行单页面应用程序的路由,然后让后端通过API表示模型的纯粹表示。 This would be particularly helpful if pursuing an HTML5 -> Mobile type of solution. 如果追求HTML5 - > Mobile类型的解决方案,这将特别有用。 I'd recommend a more tempered approach to start with depending on the skill set of you and your colleagues. 根据您和您的同事的技能,我建议采用更温和的方法开始。

The best first step would normally be to make sure to use something like a Backbone router to represent addressable front end state changes that are aligned with the primary application purpose. 最好的第一步通常是确保使用类似Backbone路由器的东西来表示与主要应用程序目的一致的可寻址前端状态更改。 If the front end is doing things like displaying a detail view which is created from an AJAX request, then rather than implement that through an event handler attached to some UI element, you should implement it using a hash segment and front end routing which the UI element links to. 如果前端正在执行显示从AJAX请求创建的详细视图,而不是通过附加到某个UI元素的事件处理程序实现它,则应使用哈希段和前端路由实现UI元素链接到。 So for instance the UI element would just be a link to something like /#/item/45 and then the router would pick that up and run the handler attached to the pattern like /#/item/{itemId} . 因此,例如,UI元素只是一个链接到/#/item/45 ,然后路由器会选择它并运行附加到模式的处理程序,如/#/item/{itemId} This better represents the state and opens the door to leveraging browser history and creating links that use the existing front end code in a clean way. 这更好地代表了状态,并打开了利用浏览器历史记录并以干净的方式创建使用现有前端代码的链接的大门。

After starting with this you can implement routers increasingly as desired. 从这开始,您可以根据需要逐步实现路由器。

Why I would use it: 为什么我会用它:

  • Cleaner UI application code. 更清洁的UI应用程序代码。 Centralized approach to the concerns. 关注问题的集中方法。 This plays big role for the complex UI applications . 这对于复杂的UI应用程序起着重要作用。

Next two items are somewhat related to the client side routing (not directly though): 接下来的两个项目与客户端路由有些相关(不是直接):

  • Ajax requests don't need to get whole page back (though still could). Ajax请求不需要返回整个页面(尽管仍然可以)。
  • Ajax requests can use compact format of the data (such as JSON) that makes request processing even faster. Ajax请求可以使用紧凑的数据格式(例如JSON),使请求处理更快。

Why I would not use it: 为什么我不会用它:

  • SEO becomes a little harder to accomplish. SEO变得有点难以完成。 Client-side routes mostly map to the client side functions, not URLs. 客户端路由主要映射到客户端功能,而不是URL。 This means, search engine will see links that don't lead to the different content pages. 这意味着,搜索引擎将看到不会导致不同内容页面的链接。 This, obviously, is not desired. 显然,这不是必需的。 To some extent, you can try to overcome it using site maps. 在某种程度上,您可以尝试使用站点地图来克服它。
  • As another overcome to the above problem, website developers sometimes have to support the same URL structure (routing) on both client-side and server-side. 另一方面克服了上述问题,网站开发人员有时必须在客户端和服务器端支持相同的URL结构(路由)。 This is more efforts for accomplishing the same end-result . 这是为了实现相同的最终结果而做出的更多努力 This, in my opinion, is a mistake, because such websites were thought as public in the beginning but were designed as private. 在我看来,这是一个错误,因为这些网站在开始时被认为是公开的,但被设计为私有的。 In the most cases, such applications could have been designed just having Ajax in mind, but not client-side routing. 在大多数情况下,这样的应用程序可能只是考虑到了Ajax,而不是客户端路由。

Conclusion: 结论:

That being said, in my opinion, client-side routing makes most sense for rich UI applications that don't need to be crawled (mostly because they are password protected). 话虽如此,在我看来,客户端路由对于不需要被抓取的丰富UI应用程序最有意义(主要是因为它们受密码保护)。

eg email, chat, enterprise applications, games, other custom applications. 例如电子邮件,聊天,企业应用,游戏,其他自定义应用程序。

On the other hand, as you may have noticed, websites intended to be crawled don't use client-side routing. 另一方面,您可能已经注意到,要爬网的网站不使用客户端路由。

eg blogs, public websites, wiki pages, etc. 例如博客,公共网站,维基页面等。

Worth mentioning, you can mix these two approaches in the same application as long as it has different sections falling into different categories mentioned above. 值得一提的是,你可以在同一个应用程序中混合这两种方法,只要它有不同的部分属于上面提到的不同类别。

For a single page application, the browser is not redrawing the whole page every time the time user makes an action and content is instead generated by Javascript and injected into the page. 对于单页面应用程序,浏览器不会在每次用户执行操作时重绘整个页面,而是通过Javascript生成内容并将其注入页面。 Client-side routes maintain state in the address bar so that a user can return to that state directly, which makes those states bookmarkable, sharable, etc. 客户端路由在地址栏中维护状态,以便用户可以直接返回到该状态,这使得这些状态可收藏,可共享等。

You still need to do server-side routing. 您仍然需要进行服务器端路由。 From the Backbone docs: 来自Backbone文档:

For example, if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly. 例如,如果您的路径为/ documents / 100,则如果浏览器直接访问该网址,则您的网络服务器必须能够提供该网页。

You will do server and client side routing. 您将执行服务器客户端路由。

Requests from your client to the server still needs to be routed on the server. 从客户端到服务器的请求仍然需要在服务器上路由。 From your server's point of view routing is still done for Ajax requests. 从服务器的角度来看,仍然为Ajax请求进行路由。

The client side router is used to route #links to your backbone app. 客户端路由器用于将#links路由到您的骨干应用程序。 So when someone clicks on a # link your router will will pick this up and action it - most probably fire an appropriate event. 因此,当有人点击#链接时,你的路由器会选择它并对其进行操作 - 最有可能触发一个适当的事件。

The the routers are very different and for now I cannot think of any circumstances where you'll use the one instead of the other. 路由器是非常不同的,现在我想不出你将使用一个而不是另一个的任何情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM