Extjs4.2 rest 与webapi数据交互

来源:互联网 发布:淘宝优惠券图片素材 编辑:程序博客网 时间:2024/04/27 17:37

这一章接着上一篇

对于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('提示', '数据删除失败');                    }                }            }        }    });

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

/// <summary>        /// 更新接口信息        /// </summary>        /// <param name="ic">需要更新的数据</param>        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的开发了,唉,刚开始熟悉,又要离开,真不舍得。

再发一点牢骚,程序员的路究竟该怎么走?我其实很迷茫,样样通,公司需要。但是对自己的长期发展不利,样样通的后果就是样样不精。但是你想精通一门也不行,公司不允许,因为他是跟项目定的。有的人说要学会拒绝,但是你敢吗?汗,我也不知道自己在说什么……迷茫中

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击