zoukankan      html  css  js  c++  java
  • Extjs4.2 rest 与webapi数据交互----顺便请教了程序员的路该怎么走

    这一章接着上一篇

    对于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的开发了,唉,刚开始熟悉,又要离开,真不舍得。

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

  • 相关阅读:
    父组件向子组件传递数据(vue.js)
    vue引入JQ的方法
    webstorm添加*.vue文件代码提醒支持webstorm支持es6vue里支持es6写法
    创建脚手架步骤
    JS严格校验身份证号
    微信小程序开发工具 常用快捷键
    GIT 常用命令
    git 操作
    通过selenium(也有Puppeter版在最后)登录网页获取特定信息
    用Django ORM实现树状结构
  • 原文地址:https://www.cnblogs.com/smiler/p/3160122.html
Copyright © 2011-2022 走看看