为什么要使用Rails使用Backbone.js的? [英] Why use Backbone.js with Rails?

查看:200
本文介绍了为什么要使用Rails使用Backbone.js的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails有通过js文件和js.erb文件,什么是一些使用框架,比如Backbone.js的?

Rails has a means to update its presentation layer via js files and js.erb files, what are some of the specific benefits of using a framework like Backbone.js?

推荐答案

这里有几个原因:

1)骨干专门使用Rails考虑到建成,很容易与的帮助集成主干上轨的。虽然淘汰赛和角的模型 - 视图 - 视图模型(MVVM)模式可以轻松地集成到一个Rails应用程序,而悄悄地,骨干的MVC架构提供了组织的水平确实必要,如果你的应用程序有很多异步页面更新。就拿这个堆栈溢出页面,一个简单的例子:

1) Backbone was built specifically with Rails in mind, and integrates easily with the help of backbone-on-rails. While the Model-View-View Model (MVVM) patterns of Knockout and Angular could easily be incorporated into a Rails app rather unobtrusively, Backbone's MVC architecture provides a level of organization that really seems necessary if your app has a lot of asynchronous page updates. Take this Stack Overflow page for a quick example:

如果你正在构建中的Rails这个问题看,你有你的问题show.html.erb,question_show.js,show.js.erb,和所有其他js.erb文件,涉及到对异步更新内容此页面(操作,如向上/向下投票,收藏,评论等)。

If you were building this question view in Rails, you'd have your question show.html.erb, question_show.js, show.js.erb, and all other js.erb files that pertain to asynchronously updating the content on this page (actions such as up/down-voting, favoriting, commenting, etc.).

在主干,一个观点是不一样show.html.erb标记模板,相反,它包含了所有相关的code在一个地方的标记资源。而不是定义在一个偏僻的所有事件监听器于是,question_show.js文件和处理各种js.erb文件中所有的AJAX更新时,的所有的事件监听和发布相关的问题,显示出资源都包含在一个地方的,骨干的问题放映视图。当然,评论可以有自己的看法和评价自己的收藏,以及其他MVC的元素,我不说也罢。但点的存在,骨干帮助您确定前端资源。

In Backbone, a view is not a markup template like show.html.erb, instead, it contains all relevant code to that markup resource in one spot. So, instead of defining all your event listeners in a remote, question_show.js file and handling all your AJAX updates in various js.erb files, all event listening and publishing relevant to the questions show resource is contained in one place, the Backbone question show view. Granted, comment could have its own view and comments their own collection, as well as other MVC elements I'm not mentioning. But point being, Backbone helps you define front-end resources.

2)选择像主干一个JavaScript框架有助于采取一些负载过你的服务器code,它真的不需要被执行服务器端。为什么呈现在服务器上html.erb模板所有的标记元素时,它可能会在客户端的浏览器来完成。为了应对安全问题,你要白名单/时,格式化你的数据库对象为JSON,并将其运送到客户端的黑名单数据库对象属性的能力。

2) Choosing a JavaScript framework like Backbone helps take some of the load off your server for code that really doesn't need to be executed server-side. Why render all your markup elements in html.erb templates on the server when it could be done in the client's browser. In response to questions of security, you have the ability to whitelist/blacklist database object attributes when formatting your database object as JSON and shipping it to the client.

3)骨干网(专)似乎给人一种自由的一个良好的数额。它提供了一组约定来帮助组织您的应用程序,但在这一天结束,这是你的框架,你正在开发。主干网的MVC框架不太单向比Rails的,但固体公约持续。

3) Backbone (specifically) seems to give a good amount of freedom. It provides a set of conventions to help organize your application, but at the end of the day it's your framework you're developing. The MVC framework of Backbone is less one-way than Rails, yet the solid conventions persist.

4)随着骨干网(不说赞成或反对其他框架),pushState很容易实现成一个框架,预计其用例。然而,pushState有它的抓取工具访问您的内容而言缺点,需要一些服务器端渲染,履带友好的方式被纳入。什么是伟大的,虽然,是可以达到同样的历史/降解使用的骨干网的外箱;他们的URL片段允许相同的功能,他们只是在那里有一个额外的#。

4) With Backbone (not speaking for or against other frameworks), pushState is easily implemented into a framework that expects its use cases. However, pushState has its downsides in terms of crawlers accessing your content, and requires some server-side rendering to be incorporated in a crawler-friendly way. What's great though, is that you can attain the same history/degradability in using Backbone out-of-box; their url fragments allow for the same functionality, they just have an extra # in there.

有很多其他的理由来使用一个框架,比如主干,它真的好像有很多替代品,因为一个框架不适合所有。但我可以证明,骨干似乎是一个很大的框架,如果你从头开始构建一个应用程序。而这也似乎很可行的,如果你想要将它整合到现有的应用程序。

There are plenty of other reasons to use a framework like Backbone, and it really seems like there are a lot of alternatives because one framework does not fit all. But for what I can attest to, Backbone seems to be a great framework if you're building an app from scratch. And it also seems very doable if you want to incorporate it into an existing application.

来源: Backbone.js的on Rails的

这篇关于为什么要使用Rails使用Backbone.js的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆