ExtJs4 json data.store 和 Rails
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是 ExtJS JsonReader 期望“users”根是一个数组。这意味着当它只是从创建和更新操作返回单个用户对象时,它无法正确解析它,因此无法正确更新模型以标记收到的更改,从而删除脏标志。
要实现此目的,您可以按照以下方式更改 Rails 操作中的格式行:(
请注意有关单个用户对象的方括号)
这将正确更新脏标志。
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:
(Note the square brackets about the single user object)
This will then properly update the dirty flags.