如果另一条记录具有相同的 属性,如何防止商店更新记录?

How to prevent a store from updating a record, if another record has the same property?

我目前正在处理一个使用 RowEditing 插件的网格,当我单击链接到网格的 'add' 按钮时,一条新的空记录将添加到网格绑定到的商店中。当我添加新行时,我可以检查用户尝试添加的值,如果此 'user_id' 值为空或重复,我会提醒用户他的重复条目并将其从商店中删除。

我有一个我无法弄清楚的边缘情况 - 当用户想要更新现有行并将 'user_id' 更改为现有行或空行时,就会出现这种边缘情况。我不想擦除这条记录,我只是想假设用户犯了错误而不更新这条记录,我想撤消his/her更新它。

这是我商店的更新方法:

update : function(com, record, successful, eOpts){
        var store = Ext.data.StoreManager.lookup('EpaymentKioskOptStores');
        var table_id = record.data.kiosk_user_id;
        var _try = false;
        var _wipe = false;
        //if the record is empty handle it
        if(table_id.trim() == null || table_id.trim() == undefined || table_id.trim() == ''){
            if(record.dirty){
                //restore to original value, and cancel update
                record.data.kiosk_user_id = record.modified.kiosk_user_id;
                _try = true;
            }else{
                store.remove(record);
                _wipe = true;
            }
        }
        //if the record's kiosk_user_id is not empty
        else{
            store.each(function(r){
                var temp = r.data.kiosk_user_id;
                //the record we are trying to enter is a new record
                if(record.phantom){
                    //make sure the record is new and if we find another with the same key we can wipe record
                    if(!r.phantom && (temp == table_id)){
                        store.remove(record);
                        _wipe = true;
                    }
                }
                else{
                    //somehow have to make sure the record and r arent the same
                    if((r.internalId != record.internalId) && (temp == table_id)){
                        record.data.kiosk_user_id = record.modified.kiosk_user_id;
                        _try = true;
                    }
                }
            });
        }
        if(_try){
            Ext.Msg.alert('Warning', 'Your change was reverted');
            store.commitChanges();
        }
        if(_wipe){
            Ext.Msg.alert('Alert', 'Your changes were discarded');
            store.commitChanges();
        }
    }

编辑:这是我为解决我的问题而实施的修复,它有点老套

检查重复记录时,请检查记录是否为 phantom。如果记录有 phantom = true 则表示这是一条新添加的记录。对于用户正在尝试编辑的现有记录,record.phantom 将为 false。

user wants to update an existing row and change the 'user_id' to an existing one or an empty one. I don't want to wipe this record, I just want to assume the user made a mistake and not update this record, I want to undo his/her update it.

  • 要检查一个空的 - 根据需要标记该字段。
  • 检查用户是否已更新为现有值:检查服务器端,如果发现重复项,return 会显示错误消息。不让用户知道为什么 his/her 更改不起作用
  • 不好

您可以在网格的(行编辑器的)validateedit 中添加此验证,而不是商店的更新方法;参见 http://docs.sencha.com/extjs/5.1/5.1.0-apidocs/#!/api/Ext.grid.plugin.RowEditing-event-validateedit

listeners: {
  validateedit: function(editor, context) {
    var newId = context.newValues.userId;
    if (!newId) {
      editor.editor.getForm().findField('userId').markInvalid('User ID is required');
      return false;
    }
    // Now iterate through the store and check all userIds against newId and return false 
    // if you see a duplicate
  }
}