Extjs4.2 rest 与webapi数据交互----顺便请教了程序员的路该怎么走

小编:管理员 504阅读 2022.09.06

这一章接着上一篇

对于Ext.data.Store 介紹 与总结,以及对以前代码的重构与优化

1.对于更新OnUpdate()函数的修改:先上代码:

function OnUpdate(record) {
    //获取要更新的数据
    var functionCode = Ext.getCmp('code').getValue();
    var FunctionName = Ext.getCmp('name').getValue();
    var IsEnabled = Ext.getCmp('isEnable').getValue();
    var Invoker = Ext.getCmp('Invoker').getValue();
    var module = Ext.getCmp('Module').getValue();
    
    record.set('FunctionCode', functionCode);
    record.set('FunctionName', FunctionName);
    record.set('IsEnabled', IsEnabled);
    record.set('Invoker', Invoker);
    record.set('Module', module);

    store.commitChanges();

    win.close();
}
复制

这里面将要修改的record记录传了过来,直接使用record的set方法对数据进行更新,然后用store的commitChanges()方法进行提交。

然后它对应的就是rest的Put方式。

2.rest方式前面讲到都是向后台传值,那么他从后台传出来的值应该怎么办呢。其实细心的读者可能会发现,上面程序是存在问题的,啥问题?如果后台对数据的操作失败了怎么办?我怎么才能知道,这就是问题所在了。

  在网上找了好久才找到方法,在store使用afterRequest,这个在api上没有,也不知道api不全或者其他原因,我试了好几种方法都不行,折腾了快一天了才搞定

大家看下代码:

store = Ext.create('Ext.data.Store', {
        autoLoad: true,
        autoSync: true,
        pageSize: 20,
        model: 'InterfaceModel',
        proxy: {
            type: 'rest',
            url: 'api/InterfaceManage',
            reader: {
                type: 'json',
                root: 'Data',
                totalProperty: 'TotolRecord',
                successProperty: 'success',
                messageProperty: 'msg'
            },
            writer: {
                type: 'json'
            },
            afterRequest: function (request, success) {
                var result = request.operation.success;

                if (request.action == 'read') {

                }

                else if (request.action == 'create') {
                    if (result) {
                        Ext.Msg.alert('添加提示', '添加成功!');
                        store.reload();
                    } else {
                        Ext.Msg.alert('添加提示', '添加失败!');
                    }
                }

                else if (request.action == 'update') {
                    if (result) {
                        Ext.Msg.alert('提示', '更新成功!');
                        store.reload();
                    }
                    else {
                        Ext.Msg.alert('提示', '更新失败!');
                    }
                }

                else if (request.action == 'destroy') {
                    if (result) {
                        Ext.Msg.alert('提示', '数据删除成功');
                        store.reload();
                    }
                    else {
                        Ext.Msg.alert('提示', '数据删除失败');
                    }
                }
            }
        }

    });
复制

这里面相应的后台程序也需要改

/// 
        /// 更新接口信息
        /// 
        /// 需要更新的数据
        public ReturnMsg Put(InterfaceConfig ic)
        {
            try
            {
                OlandHIPDBEntities db = new OlandHIPDBEntities();

                var data = from item in db.InterfaceConfig
                           where item.ID == ic.ID
                           select item;

                InterfaceConfig old = data.SingleOrDefault();

                old.FunctionCode = ic.FunctionCode;
                old.FunctionName = ic.FunctionName;
                old.Invoker = ic.Invoker;
                old.IsEnabled = ic.IsEnabled;
                old.Module = ic.Module;

                db.SaveChanges();
                return new ReturnMsg() { success = true, msg = "test" };
            }
            catch (Exception)
            {
                return new ReturnMsg() { success = false, msg = "test" };
            }
        }
复制

由于对Extjs的不理解,真的很费力,但如果找对了方法,看起来了又很简单,等今天把列过滤解决掉,这个项目就基本完活了。等下周就要进入wpf的开发了,唉,刚开始熟悉,又要离开,真不舍得。

关联标签: