這一章接着上一篇
對於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的開發了,唉,剛開始熟悉,又要離開,真不舍得。
再發一點牢騷,程序員的路究竟該怎么走?我其實很迷茫,樣樣通,公司需要。但是對自己的長期發展不利,樣樣通的后果就是樣樣不精。但是你想精通一門也不行,公司不允許,因為他是跟項目定的。有的人說要學會拒絕,但是你敢嗎?汗,我也不知道自己在說什么……迷茫中