extjs 解决使用store.sync()方法更新item有时不触发后台action的问题

问题描述:

extjs 解决使用store.sync()方法更新item有时不触发后台action,不出发后台action的原因是item的字段值没有变化

解决方法:

item.setDirty(true)

 

追查问题步骤

1、store.sync()方法               

            this_.win.store.sync({
                timeout: 1000000,
                success: function() {
                    Ext.Msg.alert('系统提示', '修改数据成功!');
                    // this_.win.gridPanel.refresh();
                    this_.win.close();
                    // me.treeRefresh(name);
                    this_.win.store.load();
                },
                failure: function() {
                    Ext.Msg.alert("系统提示", failMessage);
                    this_.win.close();
                    this_.win.store.load();
                }
            });

2、sync方法源码

sync: function(options) {
        var me = this,
            operations = {},
            toCreate = me.getNewRecords(),
            toUpdate = me.getUpdatedRecords(),
            toDestroy = me.getRemovedRecords(),
            needsSync = false;

        if (toCreate.length > 0) {
            operations.create = toCreate;
            needsSync = true;
        }

        if (toUpdate.length > 0) {
            operations.update = toUpdate;
            needsSync = true;
        }
getUpdatedRecords: function() {
        return this.data.filterBy(this.filterUpdated).items;
    },
filterBy : function(fn, scope) {
        var me = this,
            newMC  = new me.self(me.initialConfig),
            keys   = me.keys,
            items  = me.items,
            length = items.length,
            i;

        newMC.getKey = me.getKey;

        for (i = 0; i < length; i++) {
            if (fn.call(scope || me, items[i], keys[i])) {
                newMC.add(keys[i], items[i]);
            }
        }

        return newMC;
    },
filterUpdated: function(item) {
        
        return item.dirty === true && item.phantom !== true && item.isValid();
    },

3、追查到items的类型是Ext.data.Store, item的类型是Ext.data.Model,Model有setDirty和dirty方法,在触发sync方法之前,item.setDirty(true)即可

posted @ 2018-01-16 11:08  Kevin.YangPA  阅读(1291)  评论(0编辑  收藏  举报