我正在查看 Ember.js,并一直在阅读文档以尝试理解如何使用它。我明白了(很好),除了一件事。按照我对 MVC 模式的思考方式,模型是应用程序中数据的存储库。我可以看到这对于 Ember.js 中的客户端数据是如何工作的。我不明白的是如何将该数据绑定回服务器,以便如果客户端的数据发生更改,则更改会在服务器中更新。反之亦然。我一直在我的 Web 应用程序中通过 Ajax/JSON 调用来回服务器来做到这一点,我只是不知道如何使用 Ember.js 来做到这一点。
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.
发布评论
评论(4)
稍微挖掘一下 GitHub 上的 emberjs 我发现了这个:https://github.com/emberjs/data:
我还建议阅读 Ember.js Live Collections。您想要的是拥有一组知道如何与服务器端同步的模型,可能的示例代码是:
然后您可以在需要的时候调用
App.people.save()
。另请务必查看这篇文章,关于 & 的建议Ember.js 使用说明,更深入地介绍了 Ember 的服务器客户端通信,还提到了 emberjs/数据。
注意:Emberjs 数据库尚未做好生产准备,因此应谨慎使用。
Digging just a bit around emberjs on GitHub I have found this: https://github.com/emberjs/data:
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:
You'd then call
App.people.save()
at needed occasions.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.
Note: Emberjs Data Library should be used with caution for the fact that it is not production ready.
在 Ember.js 中,
Ember
对象中包含的“模型”将包含底层服务器端数据库的进一步抽象(如果您使用的话)。应用程序的控制器部分应该具有允许您检索和发送数据的方法,这些方法在需要时调用以更新模型(使用 Ajax)。这很好,因为您拥有一个模型,可以在客户端快速响应用户向应用程序提供的任何输入(击键、鼠标移动等),并选择性地选择何时对服务器端数据库进行相对昂贵的查询。这样,应用程序的某些性能不再受到外部服务器数据请求延迟的阻碍,在某些情况下,这可以让您创建响应能力接近本机应用程序的应用程序。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
基本上这意味着您使用模型加载控制器(单个或阵列),现在可以轻松地对在该模型上工作的流程进行建模(即不触及模型核心的内容) /data)在你的控制器中。对于示例博客应用程序,您可以在模型中描述Post,并为控制器添加类似的内容。
现在,您可以在前端思维方面与模型的表示进行交互通过控制器。扩展帖子与否不会改变模型,只会改变数据。
关于从服务器重新加载数据,我有两个答案给你,
I like to picture Ember.js in pairs like this
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 Post in the model and add something like that for the controller
Now you can interact with the represenation of the model in 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
我意识到这个问题有点老了,但它位于 ember.js 评分最高的页面上,所以我想我应该补充一点。
我最近一直在使用 ember-model 来处理 RESTful 数据绑定。它的花哨功能较少,但对于我的需求来说已经相当不错了。基本上,它只是扩展了模型功能,以便与通过标准 REST 接口推送 JSON 对象的服务器很好地集成。
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.
I've been using ember-model lately 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.