问题描述
自己在项目中写了一个公用的方法,方法调用的比较多,就相当于一个window window里面的内容也比较多,现在需要吧这整个方法抽离出来,方便其他的组员进行调用,初始window里面定义的form就有2个 fileSet就更多了 还有panle等等 里面也自己定义了些ID,现在不知道如何进行好的封装,刚接触EXT也没多久,求各位亲给点思路!
解决方案
//mixins类Ext.define('Canna.mixins.GridUtil', {onAfterrender : function() {var me = this;if (!this.getStore && !this.getStore()) {return;}this.showLoadingMark();this.getStore().load({callback : function() {me.hideLoadingMark();}});},onSelectionchange : function(sm, selections) {this.view.panel.down('#removeButton').setDisabled(selections.length == 0);},onAddHandler : function() {var panel = this.up('gridpanel');var store = panel.getStore();if (store.storeId == 'ext-empty-store') {store = Ext.create('Ext.data.Store', {model : 'Ext.data.Model',fields : [],proxy : 'memory'});panel.bindStore(store);}var model = store.model;var r = Ext.create(model, {});panel.getStore().insert(0, r);if (panel.rowEditingAble) {var rowEditing = panel.getEditPlugin();rowEditing.cancelEdit();rowEditing.startEdit(0, 0);}},onDeleteHandler : function() {var grid = this.up('gridpanel');var records = grid.getSelectionModel().getSelection();Ext.MessageBox.confirm('Confirm', '请确定删除!', function(buttonId) {if (buttonId == 'yes') {grid.getStore().remove(records);}});},showLoadingMark : function() {this.loadingMark = this.loadingMark || new Ext.LoadMask(this, {msg : 'loading...'});this.loadingMark.show();},hideLoadingMark : function() {this.loadingMark.hide();},getEditPlugin : function() {return this.getPlugin(this._pluginId);},addRowEditPlugin : function() {var me = this;me._pluginId = 'rowEditing' + Math.random();var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {clicksToEdit : 2,autoCancel : true,pluginId : me._pluginId});me.plugins = [ rowEditing ];}});//extend 类Ext.define('Canna.view.BaseGridPanel', {extend : 'Ext.grid.Panel',alias : 'widget.baseGridPanel',mixins : [ 'Canna.mixins.GridUtil' ],initComponent : function() {// rowEditingAble// checkboxModelAble// simpleDockAble// autoLoadStorevar me = this;if (this.rowEditingAble) {this.addRowEditPlugin();}if (this.autoLoadStore) {this.addListener('afterrender', me.onAfterrender);}if (this.checkboxModelAble) {this.selModel = Ext.create('Ext.selection.CheckboxModel', {listeners : {selectionchange : me.onSelectionchange}});}if (this.simpleDockAble) {this.dockedItems = [ {xtype : 'toolbar',items : [ {text : '添加',iconCls : 'add',handler : me.onAddHandler}, '-', {itemId : 'removeButton',text : '删除',iconCls : 'remove',disabled : true,handler : me.onDeleteHandler} ]} ];}this.callParent(arguments);}});使用mixins,或使用extend
解决方案二:
ext4.0的开发现在更加面向对象了,你可以定义一个"类",把相关方法放进去,有需要调用方法的时候,引用此"类"