gpt4 book ai didi

javascript - ember.js 和服务器

转载 作者:IT王子 更新时间:2023-10-29 03:07:01 25 4
gpt4 key购买 nike

我正在查看 Ember.js 并一直在阅读文档以尝试了解如何使用它。我明白了(很好),除了一件事。按照我在 MVC 模式中的思维方式,模型是应用程序中数据的存储库。我可以看到它如何在 Ember.js 中处理客户端数据。我不明白的是如何将该数据绑定(bind)回服务器,以便如果客户端的数据发生变化,这些变化会在服务器中更新。反之亦然。我一直在通过在我的 Web 应用程序中对服务器进行 Ajax/JSON 调用来回执行此操作,但我只是不知道如何使用 Ember.js 来执行此操作。

最佳答案

围绕 emberjs on GitHub 挖掘一点点我发现了这个: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.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.
}
});
}
});

然后您会在需要的时候调用 App.people.save()

另请务必查看这篇文章,Advice on & Instruction in the Use Of Ember.js ,这更深入地介绍了与 Ember 的服务器-客户端通信,还提到了 emberjs/data .

注意:应谨慎使用 Emberjs 数据库,因为它尚未准备好生产。

关于javascript - ember.js 和服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8948213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com