Ruby-on-rails 用于 Restful 后端的 Ember.js 或 Backbone.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12996823/
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-09-02 20:54:05  来源:igfitidea点击:

Ember.js or Backbone.js for Restful backend

ruby-on-railsrestbackbone.jsember.jsfrontend

提问by Robin Wieruch

I already know that ember.js is a more heavy weight approach in contrast to backbone.js. I read a lot of articles about both.

我已经知道 ember.js 是一种与backbone.js 相比重量更重的方法。我阅读了很多关于两者的文章。

I am asking myself, which framework works easier as frontend for a rails rest backend. For backbone.js I saw different approaches to call a rest backend. For ember it seems that I have to include some more libraries like 'data' or 'resources'. Why are there two libraries for this?

我在问自己,哪个框架更容易作为 rails rest 后端的前端。对于backbone.js,我看到了调用休息后端的不同方法。对于 ember,我似乎必须包含更多的库,例如“数据”或“资源”。为什么有两个图书馆呢?

So whats the better choice? There arent a lot of examples to connect the frontend with the backend too. Whats a good working example for a backend rest call to this:

那么什么是更好的选择呢?也没有很多例子来连接前端和后端。对于后端休息调用,这是一个很好的工作示例:

URI: ../restapi/topics GET auth credentials: admin/secrect format: json

URI:../restapi/topics GET auth 凭证:admin/secrect 格式:json

回答by Trek Glowacki

Contrary to popular opinion Ember.js isn't a 'more heavy weight approach' to Backbone.js. They're different kinds of tools that target totally different end products. Ember's sweet spot is applications where the user will keep the application open for long periods of time, perhaps all day, and interactions with the application's views or underlying data trigger deep changes in the view hierarchy. Ember is larger than Backbone, but thanks to Expires, Cache-Controlthis only matters on the the first load. After two days of daily use that extra 30k will be overshadowed by data transfers, sooner if your content involves images.

与流行观点相反,Ember.js 并不是 Backbone.js 的“重量级方法”。它们是针对完全不同的最终产品的不同类型的工具。Ember 的最佳选择是应用程序,用户将应用程序长时间保持打开状态,也许一整天,并且与应用程序视图或底层数据的交互会触发视图层次结构的深层变化。Ember 比 Backbone 大,但由于ExpiresCache-Control这仅在第一次加载时重要。每天使用两天后,额外的 30k 将被数据传输所掩盖,如果您的内容涉及图像,则更快。

Backbone is ideal for applications with a small number of states where the view hierarchy remains relatively flat and where the user tends to access the app infrequently or for shorter periods of time. Backbone's code gets to remain short and sweet because it makes the assumption that the data backing the DOM will get thrown away and both items will be memory collected: https://github.com/documentcloud/backbone/issues/231#issuecomment-4452400Backbone's smaller size also makes it better suited to brief interactions.

Backbone 非常适合具有少量状态的应用程序,其中视图层次结构保持相对平坦,并且用户倾向于不经常访问应用程序或访问时间较短。Backbone 的代码保持简短和甜蜜,因为它假设支持 DOM 的数据将被丢弃并且两个项目都将被内存收集:https: //github.com/documentcloud/backbone/issues/231#issuecomment-4452400Backbone 较小的尺寸也使其更适合简短的交互。

The apps people write in both frameworks reflect these uses: Ember.js apps include Square's web dashboard, Zendesk(at least the agent/ticketing interface), and Groupon's scheduler: all applications a user might spend all day working in.

人们在这两个框架中编写的应用程序反映了这些用途:Ember.js 应用程序包括Square 的 Web 仪表板Zendesk(至少是代理/票务界面)和Groupon 的调度程序:用户可能整天工作的所有应用程序。

Backbone apps focus more on brief or casual interactions, that are often just small sections of a larger static page: airbnb, Khan Academy, Foursquare's map and lists.

主干应用程序更侧重于简短或随意的交互,这些交互通常只是较大静态页面的一小部分:airbnb可汗学院Foursquare 的地图和列表

You canuse Backbone to make the kinds of applications that Ember targets (e.g. Rdio) by a) increasing the amount of application code you're responsible for to avoid problems like memory leaks or zombie events (I don't personally recommend this approach) or b) by adding 3rd party libraries like backbone.marionetteor Coccyx– there are many of these libraries that all try to provide similar overlapping functionality and you'll probably end up assembling your own custom framework that is bigger and requires more glue code than if you'd just used Ember.

可以使用 Backbone通过 a) 增加您负责的应用程序代码量以避免内存泄漏或僵尸事件等问题(我个人不推荐这种方法)来制作 Ember 所针对的应用程序类型(例如Rdio)或 b) 通过添加 3rd 方库,如backbone.marionetteCoccyx——有许多这些库都试图提供类似的重叠功能,您可能最终会组装自己的自定义框架,该框架更大,需要更多的胶水代码如果你刚用过 Ember。

Ultimately the question of "which to use" has two answers.

最终,“使用哪个”的问题有两个答案。

First, "Which should I use, generally, in my career": Both, just like you'll end up learning any tools specific to work you'll want to do in the future. You'd never ask "Backbone or D3?"; "Backbone or Ember" is an equally silly question.

首先,“一般来说,在我的职业生涯中我应该使用哪个”:两者,就像你最终会学习任何特定于你将来想做的工作的工具一样。你永远不会问“Backbone 还是 D3?”;“Backbone or Ember”是一个同样愚蠢的问题。

Second, "Which should I use, specifically, on my next project": Depends on the project. Both will communicate with a Rails server with equal ease. If your next project involves a mix of pages generated by the server with so-called "islands of richness" provided by JavaScript use Backbone. If your next project pushes all the interaction into the browser environment, use Ember.

其次,“我应该在下一个项目中使用哪个”:取决于项目。两者都可以轻松地与 Rails 服务器通信。如果您的下一个项目涉及服务器生成的页面与 JavaScript 提供的所谓“丰富岛”的混合,请使用 Backbone。如果您的下一个项目将所有交互推送到浏览器环境中,请使用 Ember。

回答by bengillies

To give a brief, simplified answer: for a RESTful backend, at the moment, you should use Backbone.

给出一个简短的答案:对于 RESTful 后端,目前,您应该使用 Backbone。

To give a more complex answer: It really depends on what you're doing. As others have said, Ember is designed for different things, and will appeal to a different set of people. My short answer is based on your inclusion of the RESTful requirement.

给出一个更复杂的答案:这真的取决于你在做什么。正如其他人所说,Ember 是为不同的事物而设计的,并且会吸引不同的人群。我的简短回答是基于您对 RESTful 要求的包含。

At the moment, Ember-Data (which seems to be the default persistence mechanism within Ember) is far from production ready. What this means is that it has quite a few bugs and, crucially, doesn't support nested URIs (/posts/2/comments/4556 for example). If REST is your requirement, then you'll have to work around this for the time being if you choose Ember (i.e. you'll either have to hack it in, wait, implement something like Ember-Data from scratch yourself, or use not-very-RESTful URIs). Ember-Data is not strictly part of Ember, so this is entirely possible.

目前,Ember-Data(这似乎是 Ember 中的默认持久性机制)还远未准备好生产。这意味着它有很多错误,而且至关重要的是,它不支持嵌套 URI(例如 /posts/2/comments/4556)。如果 REST 是您的要求,那么如果您选择 Ember,您将不得不暂时解决这个问题(即您要么必须破解它,等待,自己从头开始实施 Ember-Data 之类的东西,要么不使用-非常 RESTful 的 URI)。Ember-Data 严格来说并不是 Ember 的一部分,所以这是完全可能的。

The main differences between the two, aside from size, are basically:

除了大小之外,两者之间的主要区别基本上是:

Ember tries to do as much as possible for you, so that you don't have to write as much code. It is very hierarchical and, if your app is also very hierarchical, will likely be a good fit. Because it does so much for you, it can be difficult to figure out where bugs are coming from and to reason why unexpected behaviour is happening (there is a lot of "magic"). If you have an app that fits naturally into the type of app that Ember expects you to be building though, this likely won't be a problem.

Ember 尝试为您做尽可能多的事情,这样您就不必编写尽可能多的代码。它非常有层次,如果您的应用程序也非常有层次,那么它可能很适合。因为它为您做了很多事情,所以可能很难弄清楚错误来自哪里以及为什么会发生意外行为(有很多“魔法”)。如果您的应用程序自然适合 Ember 期望您构建的应用程序类型,那么这可能不会成为问题。

Backbone tries to do as little as possible for you so that you can reason about what is going on and build an architecture that fits your app (rather than building an app that fits the architecture of the framework you're using). It's a lot easier to get started with but, unless you're careful, you can end up with a mess very quickly. It doesn't do stuff like computed properties, auto-unbinding events, etc and leaves them up to you, so you will need to implement a lot of stuff yourself (or at least pick libraries that do that for you), although that is rather the whole point.

Backbone 尝试为您做的尽可能少,以便您可以推理正在发生的事情并构建适合您的应用程序的架构(而不是构建适合您正在使用的框架架构的应用程序)。上手要容易得多,但是,除非你小心,否则很快就会一团糟。它不做诸如计算属性、自动解除绑定事件之类的事情,而是由您自己决定,因此您需要自己实现很多东西(或至少选择为您做这些的库),尽管那是而是整点。

Update: It appears that, as of recently, Ember does now support nested URIs, so I suppose the question comes down to how much magic you like and whether Ember is a good fit, architecturally, for your app.

更新:最近看来,Ember 现在确实支持嵌套 URI,所以我想问题归结为您喜欢多少魔法以及 Ember 在架构上是否适合您的应用程序。

回答by Nicolas Zozol

I think that your question will soon be blocked :) There are a few contentions between the two frameworks.

我认为您的问题很快就会被阻止:) 两个框架之间存在一些争论。

Basically Backbone does not do a lot of things, and that's why I love it : you will have to code a lot, but you will code at the right place. Ember does a lot of things, so you'd better watch what it is doing.

基本上 Backbone 不会做很多事情,这就是我喜欢它的原因:你必须编写很多代码,但你会在正确的地方编写代码。Ember 做了很多事情,所以你最好看看它在做什么。

Server discussion is one of the few things that Backbone does, and it does a great job with it. So I would start with Backbone and then give a try to Ember if you are not totally satisfied.

服务器讨论是 Backbone 所做的为数不多的事情之一,它做得很好。所以我会从 Backbone 开始,如果你不完全满意,我会尝试 Ember。

You can also listen to this podcastwhere Jeremy Ashkenas, creator of Backbone, and Yehuda Katz, member of Ember, have a nice discussion

您还可以收听此播客,其中 Backbone 的创建者 Jeremy Ashkenas 和 Ember 的成员 Yehuda Katz 进行了愉快的讨论