1. 程式人生 > 其它 >Extjs4.2 rest 與webapi資料互動----順便請教了程式設計師的路該怎麼走

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的開發了,唉,剛開始熟悉,又要離開,真不捨得。

  再發一點牢騷,程式設計師的路究竟該怎麼走?我其實很迷茫,樣樣通,公司需要。但是對自己的長期發展不利,樣樣通的後果就是樣樣不精。但是你想精通一門也不行,公司不允許,因為他是跟專案定的。有的人說要學會拒絕,但是你敢嗎?汗,我也不知道自己在說什麼……迷茫中