ExtJs4 json data.store 和 Rails

发布于 2024-11-09 06:11:00 字数 1002 浏览 0 评论 0原文

我正在尝试使用 ExtJs4 和 Ruby On Rails 创建一个简单的 CRUD 应用程序。我正在尝试使用 JSON 实现 resftul 数据存储。

我在更新记录时遇到问题。请求正在发送到服务器端并且数据库正在更新,但在数据存储中我仍然有标记为“脏”的记录。当我尝试更新另一条记录时,我发送了两个请求(一个用于前一个记录,另一个用于下一个记录)。

我在创建新记录时也遇到问题。正在服务器端创建一条记录,但在我的数据存储中,它仍然标记为未保存(脏),因此在下一次更新/创建操作期间,该记录再次发送到服务器,结果我的数据库中有重复的记录。

问题是 - 如何构建有效的数据存储和相应的模型来使用 Rails 中的 Restful json?

您可以在我的 github 帐户上找到我的应用程序的来源: https://github.com/lucassus/extjs4 -account-manager

商店:https://github.com/lucassus/extjs4-account-manager/blob/master/public/javascripts/app/store/Users.js 和模型: https:// /github.com/lucassus/extjs4-account-manager/blob/master/public/javascripts/app/model/User.js

I'm trying to create a simple CRUD application using ExtJs4 and Ruby On Rails. I'm trying to implement resftul datata store with JSON.

I have problem with updating a record. The request is being sent to the server side and database is being updated but in the data store I still have record marked as "dirty". And when I'm trying to update an another record I have two request sent (one for previous record and another for the next record).

Also I have problems with creating a new record. A record is being created on the server side, but in my data store it's still marked as unsaved (dirty) so during next update / create action that record sent to the server once again and in the outcome I have duplicated records in my db.

The question is - how to build a valid data store and corresponding model to work with restful json from Rails?

Sources of my application you can find on my github account: https://github.com/lucassus/extjs4-account-manager

the store: https://github.com/lucassus/extjs4-account-manager/blob/master/public/javascripts/app/store/Users.js
and the model: https://github.com/lucassus/extjs4-account-manager/blob/master/public/javascripts/app/model/User.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-11-16 06:11:00

原因是 ExtJS JsonReader 期望“users”根是一个数组。这意味着当它只是从创建和更新操作返回单个用户对象时,它无法正确解析它,因此无法正确更新模型以标记收到的更改,从而删除脏标志。

要实现此目的,您可以按照以下方式更改 Rails 操作中的格式行:(

format.json { render :json => { :success => true, :users => [@user] } }

请注意有关单个用户对象的方括号)

这将正确更新脏标志。

The reason for this is that the ExtJS JsonReader is expecting the 'users' root to be an array. This means that when it just gets a single user object back from your create and update actions, it cannot parse it properly, so does not correctly update the models to mark the received changes, and consequently remove the dirty flag.

To make this work, you can change the format lines in your rails actions as per this:

format.json { render :json => { :success => true, :users => [@user] } }

(Note the square brackets about the single user object)

This will then properly update the dirty flags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文