shubangjun 发表于 2013-1-29 10:51:16

ext继承的两种写法

转自:http://liuna718-163-com.iteye.com/blog/805377
在使用extjs时,因为特殊的业务要求,extjs提供的功能不能满足业务逻辑,所以需要对其进行扩展,也就是所谓的继承,
在extjs中继承有两种写法
1。在ExtJS中使用Ext.extend()函数实现继承功能:
/* *扩展树的多选模式,加了写业务逻辑方法validNodeHasFolder,并重写了select方法加入了业务逻辑 **/Ext.ns("Ext.targsoft");Ext.targsoft.MultiSelectionModel = function(config){      Ext.apply(this, config);      Ext.targsoft.MultiSelectionModel.superclass.constructor.call(this);};      Ext.extend(Ext.targsoft.MultiSelectionModel , Ext.tree.MultiSelectionModel,{      validCount : 0 ,            select : function(node, e, keepExisting){          if(keepExisting !== true){            this.clearSelections(true);          }          if(this.isSelected(node)){            this.lastSelNode = node;            return node;          }          if(!node.isLeaf() && keepExisting ){            return ;            }                  if(keepExisting && node.isLeaf() && this.validCount == 0 ){            alert(this.validCount );            this.validNodeHasFolder();          }                           this.selNodes.push(node);          this.selMap = node;          this.lastSelNode = node;          node.ui.onSelectedChange(true);          this.fireEvent("selectionchange", this, this.selNodes);          return node;                        },      //判断选择的节点中是否含有文件夹,如果有那么就进行反选      validNodeHasFolder : function(){          this.validCount = 1 ;          for(var i=0 ; i< this.selNodes.length ; i++){            if(!this.selNodes.isLeaf()){                  this.unselect(this.selNodes);            }          }      }    });

2.Extjs中替换constructor,写法如下:
Ext.ns('Ext.formdesign');Ext.formdesign.FormSort = Ext.extend(Ext.Window ,{          constructor : function(_cfg){            Ext.apply(this,_cfg);            this.grid = this.createGrid();            Ext.formdesign.FormSort.superclass.constructor.call(this,{                  title : '表单类别' ,                  width : 280 ,                  height : 270 ,                  closeAction : 'close' ,                  items : ,                  buttons : [                      {                        text : '关闭' ,                        scope : this ,                        handler : function(){                              this.close();                        }                      }                  ]            })      },            //创建表格      createGrid : function(){          //搜索          var searchObjRecs = Ext.data.Record.create([             {name: 'formSortUuid', mapping: 'formSortUuid'},            {name: 'sortName', mapping: 'sortName'},             {name: 'description', mapping: 'description'}          ]);                  var searchObjSm = new Ext.grid.CheckboxSelectionModel({header : '' ,singleSelect: true});          var searchObjStore = new Ext.data.Store({                  url : this.contextPath+'/formsort.do?method=getFormSortDatas',                  autoLoad : false ,                  reader: new Ext.data.JsonReader(                      {                         totalProperty: "total",                         root: 'data',                         id: 'formSortUuid'                     },searchObjRecs )                           });         /*          var pagingBar = new Ext.PagingToolbar({             pageSize: rowLimit_1,             store: searchObjStore,             displayInfo: true,             displayMsg: '总数: {2}',             emptyMsg: '没有数据'               });         */                  var searchObjGrid = new Ext.grid.GridPanel({                  id: 'searchObjGridId',                  store: searchObjStore,                  height : 202 ,                  autoScroll : true ,                  cm: new Ext.grid.ColumnModel([                      searchObjSm,                      {header: "编号", width: 100, dataIndex: 'formSortUuid', sortable: true,hidden: true},                      {header: "名称", width: 105, dataIndex: 'sortName', sortable: true} ,                      {header: "描述", width: 105, dataIndex: 'description', sortable: true}                   ]),                  tbar : [                      {                        text :'添加',                        handler : this.createRecord.createDelegate(this)                      },'-',{                        text :'修改',                        handler : this.updateRecord.createDelegate(this)                      },'-',{                        text :'删除',                        handler : this.delteRecord.createDelegate(this)                      }                  ] ,                  frame: false,                  loadMask:{msg:"数据加载中...."},                  sm:searchObjSm         });                  return searchObjGrid ;      } ,      //新建      createRecord : function(){          this.getWindow().show();          this.getWindow().setTitle('新建表单类别');          this.getForm().baseParams = {            create : true         } ;      } ,      //修改      updateRecord : function(){          var r = this.getGridSeleted();          if(r){            this.getWindow().show();            this.getWindow().setTitle('修改表单类别');            this.getForm().baseParams = {                  create : false               } ;            this.getForm().loadRecord(r);          }      } ,      //删除记录       delteRecord: function(){          var sort = this.protectId ? this.protectId : 'temp' ;          var rec = this.getGridSeleted();          if(rec){            Ext.Msg.confirm('提示','确认删除<b>'+rec.get('sortName')+'</b>吗?',function(btn){                  if(btn == 'yes'){                      if(rec.get('formSortUuid') == sort){                        Ext.Msg.alert('提示','<b>'+rec.get('sortName')+'</b>是系统默认类别不能删除!');                      }else{                        Ext.Ajax.request({                              url : this.contextPath+'/formsort.do?method=delFormSort',                              params : {sortId : rec.get('formSortUuid')} ,                                 scope : this ,                              callback :function(options ,success ,response ){                                  if(success){                                    var text = response.responseText ;                                    if(text == '1'){                                          Ext.Msg.alert('提示','删除成功!',function(btn){                                              if(btn == 'ok'){                                                this.grid.getSelectionModel().clearSelections();                                                   this.grid.getStore().reload();                                              }                                          },this);                                    }else if(text == '2'){                                          Ext.Msg.alert('提示','<b>'+rec.get('sortName')+'</b>已经被业务对象使用不能进行删除!');                                    }else{                                          Ext.Msg.alert('提示','删除数据失败!');                                    }                                  }else{                                    Ext.Msg.alert('提示','删除数据失败!');                                  }                              }                        });                      }                  }            },this)          }      },            createForm : function(){          var form = new Ext.form.FormPanel({                  frame : true ,                  defaultType : 'textfield' ,                  buttonAlign : 'center' ,                  labelAlign : 'right' ,                  labelWidth : 70 ,                  items : [                      {                        fieldLabel : 'id' ,                        xtype : 'hidden',                        name : 'formSortUuid'                      },                      {                        fieldLabel : '名称' ,                        name : 'sortName'                      },{                        fieldLabel : '描述' ,                        xtype : 'textarea' ,                        name : 'description'                      }                  ] ,                  buttons : [                      {                        text : '确定' ,                        scope : this ,                        handler : function(){                              form.getForm().submit({                                    url: this.contextPath+'/formsort.do?method=saveOrUpdateFormSort',                                    waitTitle : '请等待' ,                                    waitMsg: '正在提交中',                                    scope : this ,                                    success:function(form,action){                                          this.getForm().reset();                                          this.grid.getSelectionModel().clearSelections();                                           this.grid.getStore().reload();                                          this.getWindow().hide();                                    },                                    failure:function(form,action){                                          Ext.Msg.alert('提示','保存失败!');                                    }                           });                        }                                                } , {                        text : '取消' ,                        scope : this ,                        handler : function(){                              this.getForm().reset();                              this.getWindow().hide();                        }                      }                     ]                  });            return form ;      } ,            getForm : function(){          var form = this.getFormPanel().getForm();          return form ;      } ,            getFormPanel : function(){          if(!this.gridForm){            this.gridForm = this.createForm();          }         return this.gridForm ;      } ,            createWindow : function(){          var form = this.getFormPanel();          var win = new Ext.Window({                  title : 'info' ,                  closeAction : 'hide' ,                  width : 300 ,                  height : 180 ,                  modal : true ,                  items :                         });          return win ;      },            getWindow : function(){          if(!this.gridWindow){            this.gridWindow = this.createWindow();          }         return this.gridWindow ;      } ,                  //取到grid选择的记录      getGridSeleted : function(){          var sm = this.grid.getSelectionModel();                  if(sm.hasSelection()){            return sm.getSelected() ;          }else{            Ext.Msg.alert('提示','请选择一条记录!');            return null ;          }      }   })
页: [1]
查看完整版本: ext继承的两种写法