ExtJS 4表单更新商店中新创建的记录的问题-loadRecord updateRecord [英] ExtJS 4 problems with form updating a new created record in a store - loadRecord updateRecord
问题描述
我在将表格保存到商店时遇到问题.表单加载用于编辑现有记录的记录,或者对于新记录为空白.编辑现有记录可以正常工作.创建一个新的也很好.如果我想编辑&会遇到问题更新新创建的记录,而无需关闭并重新打开表单.
I have problems with a form saving to a store. The form loads a record for editing a existing record, or is blank for a new record. Editing a existing record works fine. Creating a new one works fine as well. I get problems if I want to edit & update a newly created record without closing and re-opening the form.
我想问题是商店内部记录的ID是通过远程服务器分配的.该表单持有该模型的副本,并且没有注意到ID的更改.
I guess the problem is that the id of the record inside the store is assigned through the remote server. The form is holding a copy of the model and is not taking any notice of the changing id.
有什么想法可以使表单和商店保持同步?
Any ideas how to keep the form and the store in sync?
以下代码用于保存:
var basicForm = this.up('form').getForm(),
record = basicForm.getRecord();
if (basicForm.isValid()) {
if (!record) {
record = Ext.data.StoreManager.lookup('theModel').add( this.up('form').getForm().getFieldValues())[0];
basicForm.loadRecord(record);
} else {
basicForm.updateRecord(record);
}
}
推荐答案
要继续您的示例,您可以在商店中监听write事件:
To continue with your example, you can listen for the write event on store:
var basicForm = this.up('form').getForm(),
record = basicForm.getRecord(),
store = Ext.data.StoreManager.lookup('theModel'); // We'll add a field for simplicity
store.on('write', onTheModelWrite);
if (basicForm.isValid()) {
if (!record) {
record = Ext.data.StoreManager.lookup('theModel').add(this.up('form').getForm().getFieldValues())[0];
basicForm.loadRecord(record);
} else {
basicForm.updateRecord(record);
}
}
var onTheModelWrite = function(s, o)//Here Ext passes the store and the options passed to save()
{
record = s.getAt( s.indexOf(record) ); //We use getAt() because we don't know the id
basicForm.loadRecord(record);
}
您当然应该将所有这些考虑在内,但是,希望您能理解.
You should put all this in scope, of course but, hopefully, you get the idea.
这篇关于ExtJS 4表单更新商店中新创建的记录的问题-loadRecord updateRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!