[英]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:
以下是您的选择:
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.
它肯定不会很快消失,并且有充分的理由:如果你没有开发一个单页应用程序,这是很好的,大多数人都没有。
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来替换服务器端路由器。
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:
以下是一些例子:
/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路由,将视图更改为用户的“编辑”模式可以编辑他的个人资料信息 /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 优点
exception case 例外情况
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: 为什么我会用它:
Next two items are somewhat related to the client side routing (not directly though): 接下来的两个项目与客户端路由有些相关(不是直接):
Why I would not use it: 为什么我不会用它:
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.