Javascript ember.js 和服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8948213/
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
ember.js and the server
提问by writes_on
I'm looking at Ember.js and have been reading the docs to try and understand how to use it. I get it (pretty well), except for one thing. To my way of thinking in the MVC pattern, the Model is the repository for data in the application. I can see how that works for client side data in Ember.js. What I don't get is how to tie that data back to the server so that if data changes at the client, the changes are updated in server. And vice versa. I've been doing this by in in my web applications making Ajax/JSON calls to back and forth to the server, I'm just not getting how to do that using Ember.js.
我正在查看 Ember.js 并一直在阅读文档以尝试了解如何使用它。我明白了(很好),除了一件事。按照我在 MVC 模式中的思维方式,模型是应用程序中数据的存储库。我可以看到 Ember.js 中的客户端数据是如何工作的。我不明白的是如何将该数据绑定回服务器,以便如果客户端的数据发生更改,则更改会在服务器中更新。反之亦然。我一直在通过在我的 Web 应用程序中对服务器进行 Ajax/JSON 调用来回执行此操作,我只是不知道如何使用 Ember.js 来执行此操作。
回答by Misha Reyzlin
Digging just a bit around emberjs on GitHubI have found this: https://github.com/emberjs/data:
在 GitHub 上对 emberjs 进行了一些挖掘,我发现了这个:https: //github.com/emberjs/data:
Ember Data is a library for loading models from a persistence layer (such as a JSON API), updating those models, then saving the changes. It provides many of the facilities you'd find in server-side ORMs like ActiveRecord, but is designed specifically for the unique environment of JavaScript in the browser.
Ember Data 是一个库,用于从持久层(例如 JSON API)加载模型,更新这些模型,然后保存更改。它提供了许多您可以在服务器端 ORM(如 ActiveRecord)中找到的功能,但它是专门为浏览器中 JavaScript 的独特环境而设计的。
I'd also suggest reading Ember.js Live Collections. What you want is to have a collection of models that will know how to sync with server side, possible example code is:
我还建议阅读Ember.js Live Collections。您想要的是拥有一组知道如何与服务器端同步的模型,可能的示例代码是:
// our model
App.Person = Ember.Object.extend();
App.people = Ember.ArrayController.create({
content: [],
save: function () {
// assuming you are using jQuery, but could be other AJAX/DOM framework
$.post({
url: "/people",
data: JSON.stringify( this.toArray() ),
success: function ( data ) {
// your data should already be rendered with latest changes
// however, you might want to change status from something to "saved" etc.
}
});
}
});
You'd then call App.people.save()
at needed occasions.
然后你会App.people.save()
在需要的时候打电话。
Also be sure to check out this article, Advice on & Instruction in the Use Of Ember.js, that goes deeper into server-client communication with Ember and also mentions emberjs/data.
另外一定要查看这篇文章,使用 Ember.js 的建议和说明,它更深入地介绍了与 Ember 的服务器-客户端通信,并提到了emberjs/data。
Note: Emberjs Data Library should be used with caution for the fact that it is not production ready.
注意:Emberjs 数据库应谨慎使用,因为它尚未准备好用于生产。
回答by Peter Behr
In Ember.js, the "model" contained in the Ember
object will contain a further abstraction of an underlying server side database, if you're using one. The controller portion of the application then should have methods which allow you to retrieve and send data which are called when needed in order to update the model (using Ajax). This is nice because you have a model which can respond quickly on the client side to any input a user provides the application (keystrokes, mouse movements, whatever) and selectively choose when to make relatively costly queries to a server side database, for example. This way some of the performance of the app is no longer hindered by the latency of data requests to an an external server, which in some cases can allow you to create applications whose responsiveness approaches that of native apps.
在 Ember.js 中,Ember
对象中包含的“模型”将包含底层服务器端数据库的进一步抽象(如果您正在使用)。然后,应用程序的控制器部分应该具有允许您检索和发送数据的方法,这些方法在需要时调用以更新模型(使用 Ajax)。这很好,因为您有一个模型,它可以在客户端快速响应用户向应用程序提供的任何输入(击键、鼠标移动等),并选择性地选择何时对服务器端数据库进行相对昂贵的查询。这样,应用程序的某些性能不再受到对外部服务器的数据请求延迟的阻碍,
回答by Ronald
I like to picture Ember.js in pairs like this
我喜欢像这样成对地描绘 Ember.js
- Views and Templates are correlated (obviously), tweak the Views-Class to control the Template (like the classNames)
- Router and Routes work a bit like the controller in MVC. They are responsible for routing the request to the correct endpoint
- Controller and Model are model centric, one (the Model) describes the data you will handle in your application while the controller behaves like a kind of proxy (or decorator, if that's more up your alley). Templates will hook up to the controller for example and
- 视图和模板是相关的(显然),调整视图类来控制模板(如类名)
- 路由器和路由有点像 MVC 中的控制器。他们负责将请求路由到正确的端点
- 控制器和模型是以模型为中心的,一个(模型)描述了您将在应用程序中处理的数据,而控制器的行为就像一种代理(或装饰器,如果这更适合您的话)。例如,模板将连接到控制器和
Basically that means you load up your controller (single or array) with a model and can now easily model the processes working on that model (i.e. the stuff that does not touch the model in its core/data) in your controller. For an example blog application you would describe the Postin the model and add something like that for the controller
基本上,这意味着您使用模型加载控制器(单个或数组),现在可以轻松地在控制器中对在该模型上工作的流程(即不涉及其核心/数据中的模型的东西)进行建模。对于示例博客应用程序,您将在模型中描述Post并为控制器添加类似的内容
App.PostController = Ember.ObjectController.extend({
content: null,
// initial value
isExpanded: false,
expand: function() {
this.set('isExpanded', true)
},
contract: function() {
this.set('isExpanded', false)
}
});
Now you can interact with the represenation of the modelin terms of frontend thinking through the controller. Expanding a post or not does not change the model, only changing the data does.
现在,您可以通过控制器在前端思维方面与模型的表示进行交互。是否扩展帖子不会改变模型,只会改变数据。
In terms of reloading data from the server, I have two answers for you
在从服务器重新加载数据方面,我有两个答案给你
- I found this articlequite helpful in understanding the connections (and simple polling, albeit simple)
- If you're using Rails, you will get lucky with upcoming Rails 4 Live, see this post and demofor the juicy parts
回答by pfooti
I realize this is a bit old of a question, but it's on the highest-rated page for ember.js, so I figured I'd add a bit.
我意识到这是一个有点老的问题,但它位于 ember.js 评分最高的页面上,所以我想我会补充一点。
I've been using ember-modellately to handle RESTful data binding. It has fewer bells and whistles, but for my needs it's pretty decent. Basically, it just extends the model functionality to integrate reasonably well with a server pushing JSON objects through a standard REST interface.
我最近一直在使用ember-model来处理 RESTful 数据绑定。它的花里胡哨较少,但对于我的需求来说,它相当不错。基本上,它只是扩展了模型功能,以便与通过标准 REST 接口推送 JSON 对象的服务器很好地集成。