ExtJS中的面向对象设计,组件化编程思想

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
  2InBlock.gif * @author: Lilf
  3InBlock.gif * Description: ExtJS中的面向对象设计,组件化变成思想
  4ExpandedBlockEnd.gif */

  5 ExpandedBlockStart.gifContractedBlock.gif /**/ /***************************扩展VTypes类,增加年龄的验证****************************/
  6 ExpandedBlockStart.gifContractedBlock.gifExt.apply(Ext.form.VTypes,  dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    "age"function(_v)dot.gif{
  8ExpandedSubBlockStart.gifContractedSubBlock.gif        if (/^\d+$/.test(_v)) dot.gif{
  9InBlock.gif            var intExp = parseInt(_v);
 10InBlock.gif            if (intExp < 200
 11InBlock.gif                return true;
 12ExpandedSubBlockEnd.gif        }

 13InBlock.gif        return false;
 14ExpandedSubBlockEnd.gif    }
,
 15InBlock.gif    ageText: "请输入正确的年龄格式,如:23"
 16ExpandedBlockEnd.gif}
);
 17 ExpandedBlockStart.gifContractedBlock.gif /**/ /***************************继承自FormPanel的表单组件,用来构件Window***************************/
 18 ExpandedBlockStart.gifContractedBlock.gifPersonInfoFormPanel  =  Ext.extend(Ext.form.FormPanel,  dot.gif {
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    constructor: function()dot.gif{
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        PersonInfoFormPanel.superclass.constructor.apply(this, [dot.gif{
 21InBlock.gif            baseCls: "x-plain",
 22InBlock.gif            buttonAlign: "right",
 23InBlock.gif            labelWidth: 30,
 24InBlock.gif            defaultType: "textfield",
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            defaults: dot.gif{
 26InBlock.gif                anchor: "95%",
 27InBlock.gif                labelStyle: "text-align:right"
 28ExpandedSubBlockEnd.gif            }
,
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            items: [dot.gif{
 30InBlock.gif                fieldLabel: "姓名",
 31InBlock.gif                name: "name"
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            }
dot.gif{
 33InBlock.gif                fieldLabel: "年龄",
 34InBlock.gif                name: "age",
 35InBlock.gif                vtype: "age"//验证年龄(通过vtype类型来验证)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            }
dot.gif{
 37InBlock.gif                xtype: "combo",
 38InBlock.gif                mode: "local",//本地数据
 39InBlock.gif                readOnly: true,
 40InBlock.gif                fieldLabel: "性别",
 41InBlock.gif                displayField: "sex",//显示下拉框的内容
 42InBlock.gif                triggerAction: "all",//在选择时,显示所有的项
 43InBlock.gif                value: "",//默认值
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                store: new Ext.data.SimpleStore(dot.gif{
 45InBlock.gif                    fields: ["sex"],
 46InBlock.gif                    data: [[""], [""]]
 47ExpandedSubBlockEnd.gif                }
),
 48InBlock.gif                name: "sex"//绑定字段
 49ExpandedSubBlockEnd.gif            }
]
 50InBlock.gif        
 51InBlock.gif        
 52ExpandedSubBlockEnd.gif        }
])
 53ExpandedSubBlockEnd.gif    }
,
 54InBlock.gif    //---以下为PersonInfoFormPanel类对外提供的方法---
 55ExpandedSubBlockStart.gifContractedSubBlock.gif    getValues: function()dot.gif{
 56InBlock.gif        if (this.getForm().isValid()) 
 57InBlock.gif            return new Ext.data.Record(this.getForm().getValues());
 58InBlock.gif        else 
 59InBlock.gif            throw new Error("验证没有通过");//自定义异常
 60ExpandedSubBlockEnd.gif    }
,
 61ExpandedSubBlockStart.gifContractedSubBlock.gif    setValues: function(_r)dot.gif{
 62InBlock.gif        this.getForm().loadRecord(_r);
 63ExpandedSubBlockEnd.gif    }
,
 64ExpandedSubBlockStart.gifContractedSubBlock.gif    reset: function()dot.gif{
 65InBlock.gif        this.getForm().reset();
 66ExpandedSubBlockEnd.gif    }

 67InBlock.gif    
 68InBlock.gif    
 69ExpandedBlockEnd.gif}
);
 70 ExpandedBlockStart.gifContractedBlock.gif /**/ /*************继承自Window的基类,insertWindow与updateWindow都由此继承****************/
 71 ExpandedBlockStart.gifContractedBlock.gifbaseWindow  =  Ext.extend(Ext.Window,  dot.gif {
 72InBlock.gif    form: null,
 73ExpandedSubBlockStart.gifContractedSubBlock.gif    constructor: function()dot.gif{
 74InBlock.gif        this.form = new PersonInfoFormPanel();//实例化PersonInfoFormPanel类
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        baseWindow.superclass.constructor.apply(this, [dot.gif{
 76InBlock.gif            plain: true,
 77InBlock.gif            width: 350,
 78InBlock.gif            //title: "新增人员",
 79InBlock.gif            modal: true,
 80InBlock.gif            resizable: false,
 81InBlock.gif            closeAction: "hide",
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            defaults: dot.gif{
 83InBlock.gif                style: "padding:5px"
 84ExpandedSubBlockEnd.gif            }
,
 85InBlock.gif            items: this.form,
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            buttons: [dot.gif{
 87InBlock.gif                text: "确 定",
 88InBlock.gif                handler: this.onSubmitClick,//提交事件调用
 89InBlock.gif                scope: this
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            }
dot.gif{
 91InBlock.gif                text: "取 消",
 92InBlock.gif                handler: this.onCancelClick,//取消事件调用
 93InBlock.gif                scope: this
 94InBlock.gif            
 95ExpandedSubBlockEnd.gif            }
]
 96ExpandedSubBlockEnd.gif        }
]);
 97InBlock.gif        //给insertWindow对象添加事件(事件冒泡)
 98InBlock.gif        this.addEvents("submit");
 99ExpandedSubBlockEnd.gif    }
,
100InBlock.gif    //提交事件处理函数
101ExpandedSubBlockStart.gifContractedSubBlock.gif    onSubmitClick: function()dot.gif{
102ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
103InBlock.gif            //发布事件
104InBlock.gif            this.fireEvent("submit"thisthis.form.getValues());//调用PersonInfoFormPanel类中自定义的方法getValues
105InBlock.gif            this.close();
106InBlock.gif            
107ExpandedSubBlockEnd.gif        }
 
108ExpandedSubBlockStart.gifContractedSubBlock.gif        catch (_err) dot.gif{
109InBlock.gif            Ext.Msg.alert("系统提示", _err.description);//扑捉自定义错误或异常
110ExpandedSubBlockEnd.gif        }

111ExpandedSubBlockEnd.gif    }
,
112InBlock.gif    //取消事件处理函数
113ExpandedSubBlockStart.gifContractedSubBlock.gif    onCancelClick: function()dot.gif{
114InBlock.gif        this.close();
115ExpandedSubBlockEnd.gif    }
,
116InBlock.gif    //重置与隐藏事件处理函数
117ExpandedSubBlockStart.gifContractedSubBlock.gif    close: function()dot.gif{
118InBlock.gif        this.form.reset();
119InBlock.gif        this.hide();
120ExpandedSubBlockEnd.gif    }

121InBlock.gif    
122ExpandedBlockEnd.gif}
);
123 ExpandedBlockStart.gifContractedBlock.gif /**/ /******************insertWindow类****************************/
124 ExpandedBlockStart.gifContractedBlock.gifinsertWindow  =  Ext.extend(baseWindow,  dot.gif {
125InBlock.gif    title: "新增人员"
126ExpandedBlockEnd.gif}
);
127 ExpandedBlockStart.gifContractedBlock.gif /**/ /****************updateWindow类******************************/
128 ExpandedBlockStart.gifContractedBlock.gifupdateWindow  =  Ext.extend(baseWindow,  dot.gif {
129InBlock.gif    title: "修改人员",
130ExpandedSubBlockStart.gifContractedSubBlock.gif    load: function(_r)dot.gif{
131InBlock.gif        this.form.setValues(_r);
132ExpandedSubBlockEnd.gif    }

133ExpandedBlockEnd.gif}
);
134 ExpandedBlockStart.gifContractedBlock.gif /**/ /*******根据上面组件创建新的GridPanel类,它就像我们根据不同的零件设计出来的汽车************
135InBlock.gif * ExtJs自定义PersonListGridPanel类
136InBlock.gif * 该类继承自GridPanel[使用Ext.extend(superClass,override Object)方法实现继承],
137InBlock.gif * 并override了该类的构造函�hu数
138InBlock.gif * 构造函数内部继承自GridPanel的构造函数[apply(this,arguments)实现继承]
139InBlock.gif * 该类实现了如何对外部公布一个事�件
140InBlock.gif * 在构造函数中添加一个事件[this.addEvents("事件名称")]
141InBlock.gif * 然后使用this.fireEvent("事件名称",参数)来发布此事�件
142InBlock.gif * 最后在客户端调用的时候来订阅该事�jian件
143ExpandedBlockEnd.gif */

144 ExpandedBlockStart.gifContractedBlock.gifPersonListGridPanel  =  Ext.extend(Ext.grid.GridPanel,  dot.gif {
145InBlock.gif    _window: null,
146InBlock.gif    _updateWin: null,
147ExpandedSubBlockStart.gifContractedSubBlock.gif    constructor: function(_url)dot.gif{
148InBlock.gif        this._window = new insertWindow();//insertWindow对象引用
149InBlock.gif        this._updateWin = new updateWindow();//updateWindow对象引用
150ExpandedSubBlockStart.gifContractedSubBlock.gif        PersonListGridPanel.superclass.constructor.apply(this, [dot.gif{
151InBlock.gif            renderTo: Ext.getBody(),
152InBlock.gif            width: 550,
153InBlock.gif            height: 200,
154InBlock.gif            frame: true,
155InBlock.gif            layout: "form",
156InBlock.gif            //工具栏
157ExpandedSubBlockStart.gifContractedSubBlock.gif            tbar: [dot.gif{
158InBlock.gif                text: "add",
159ExpandedSubBlockStart.gifContractedSubBlock.gif                handler: function()dot.gif{
160InBlock.gif                    this._window.show();
161ExpandedSubBlockEnd.gif                }
,
162InBlock.gif                scope: this
163ExpandedSubBlockStart.gifContractedSubBlock.gif            }
"-"dot.gif{
164InBlock.gif                text: "update",
165ExpandedSubBlockStart.gifContractedSubBlock.gif                handler: function()dot.gif{
166InBlock.gif                    this._updateWin.show();
167ExpandedSubBlockStart.gifContractedSubBlock.gif                    try dot.gif{
168InBlock.gif                        this._updateWin.load(this.getSelected());
169ExpandedSubBlockEnd.gif                    }
 
170InBlock.gif                    
171InBlock.gif                    
172ExpandedSubBlockStart.gifContractedSubBlock.gif                    catch (_err) dot.gif{
173InBlock.gif                        Ext.Msg.alert("系统提示", _err.description);
174InBlock.gif                        this._updateWin.close();
175ExpandedSubBlockEnd.gif                    }

176ExpandedSubBlockEnd.gif                }
,
177InBlock.gif                scope: this
178ExpandedSubBlockStart.gifContractedSubBlock.gif            }
"-"dot.gif{
179InBlock.gif                text: "delete",
180InBlock.gif                handler: this.onRemovePerson,
181InBlock.gif                scope: this
182ExpandedSubBlockEnd.gif            }
],
183InBlock.gif            enableColumnMove: false,
184InBlock.gif            //列模板
185ExpandedSubBlockStart.gifContractedSubBlock.gif            columns: [dot.gif{
186InBlock.gif                header: "Name",
187InBlock.gif                menuDisabled: true,
188InBlock.gif                dataIndex: "name"
189ExpandedSubBlockStart.gifContractedSubBlock.gif            }
dot.gif{
190InBlock.gif                header: "Age",
191InBlock.gif                menuDisabled: true,
192InBlock.gif                dataIndex: "age"
193ExpandedSubBlockStart.gifContractedSubBlock.gif            }
dot.gif{
194InBlock.gif                header: "Sex",
195InBlock.gif                menuDisabled: true,
196InBlock.gif                dataIndex: "sex"
197ExpandedSubBlockEnd.gif            }
],
198InBlock.gif            //数据源
199ExpandedSubBlockStart.gifContractedSubBlock.gif            store: new Ext.data.JsonStore(dot.gif{
200InBlock.gif                autoLoad: true,
201InBlock.gif                url: _url,
202InBlock.gif                fields: ["name""age""sex"]
203ExpandedSubBlockEnd.gif            }
),
204InBlock.gif            //选中模板
205ExpandedSubBlockStart.gifContractedSubBlock.gif            selModel: new Ext.grid.RowSelectionModel(dot.gif{
206InBlock.gif                singleSelect: true,
207ExpandedSubBlockStart.gifContractedSubBlock.gif                listeners: dot.gif{
208ExpandedSubBlockStart.gifContractedSubBlock.gif                    "rowselect"dot.gif{
209InBlock.gif                        fn: this.onRowSelected,
210InBlock.gif                        scope: this
211ExpandedSubBlockEnd.gif                    }

212ExpandedSubBlockEnd.gif                }

213ExpandedSubBlockEnd.gif            }
)
214InBlock.gif        
215ExpandedSubBlockEnd.gif        }
]);
216InBlock.gif        //添加事件
217InBlock.gif        this.addEvents("rowselect");
218InBlock.gif        //事件订阅
219InBlock.gif        this._window.on("submit"this.onInsertWinSubmit, this);
220InBlock.gif        this._updateWin.on("submit"this.onUpdateWinSubmit, this);
221ExpandedSubBlockEnd.gif    }
,
222InBlock.gif    //----- 以下为自定义方法---------
223InBlock.gif    //获得选中的记录
224ExpandedSubBlockStart.gifContractedSubBlock.gif    getSelected: function()dot.gif{
225InBlock.gif        var _sm = this.getSelectionModel();
226InBlock.gif        if (_sm.getCount() == 0
227InBlock.gif            throw new Error("你没有选中任何记录,请选择一条记录后重试dot.gif");
228InBlock.gif        return _sm.getSelected();
229ExpandedSubBlockEnd.gif    }
,
230InBlock.gif    //插入一条记录
231ExpandedSubBlockStart.gifContractedSubBlock.gif    insert: function(_r)dot.gif{
232InBlock.gif        this.getStore().add(_r);
233ExpandedSubBlockEnd.gif    }
,
234InBlock.gif    //更新选中的记录
235ExpandedSubBlockStart.gifContractedSubBlock.gif    update: function(_r)dot.gif{
236ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
237InBlock.gif            var _rs = this.getSelected();
238InBlock.gif            var _data = _rs.data;
239ExpandedSubBlockStart.gifContractedSubBlock.gif            for (var _i in _data) dot.gif{
240InBlock.gif                _rs.set(_i, _r.get(_i));
241ExpandedSubBlockEnd.gif            }
;
242InBlock.gif            _rs.commit();
243ExpandedSubBlockEnd.gif        }
 
244ExpandedSubBlockStart.gifContractedSubBlock.gif        catch (_err) dot.gif{
245InBlock.gif        
246ExpandedSubBlockEnd.gif        }

247InBlock.gif        
248ExpandedSubBlockEnd.gif    }
,
249InBlock.gif    //删除选中的记录
250ExpandedSubBlockStart.gifContractedSubBlock.gif    remove: function()dot.gif{
251ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
252InBlock.gif            var _rs = this.getSelected();
253ExpandedSubBlockStart.gifContractedSubBlock.gif            Ext.Msg.confirm("系统提示""你确定删除吗?"function(_btn)dot.gif{
254InBlock.gif                if (_btn == "yes"
255InBlock.gif                    this.getStore().remove(_rs);
256ExpandedSubBlockEnd.gif            }
this);
257ExpandedSubBlockEnd.gif        }
 
258ExpandedSubBlockStart.gifContractedSubBlock.gif        catch (_err) dot.gif{
259InBlock.gif            Ext.Msg.alert("系统提示", _err.description);
260ExpandedSubBlockEnd.gif        }

261ExpandedSubBlockEnd.gif    }
,
262InBlock.gif    //-------以下为自定义事件处理函数------------
263InBlock.gif    //添加事件
264ExpandedSubBlockStart.gifContractedSubBlock.gif    onInsertWinSubmit: function(_win, _r)dot.gif{
265InBlock.gif        this.insert(_r);
266ExpandedSubBlockEnd.gif    }
,
267InBlock.gif    //修改事件
268ExpandedSubBlockStart.gifContractedSubBlock.gif    onUpdateWinSubmit: function(_win, _r)dot.gif{
269InBlock.gif        this.update(_r);
270ExpandedSubBlockEnd.gif    }
,
271InBlock.gif    //删除事件
272ExpandedSubBlockStart.gifContractedSubBlock.gif    onRemovePerson: function()dot.gif{
273InBlock.gif        this.remove();
274ExpandedSubBlockEnd.gif    }
,
275InBlock.gif    //选中事件
276ExpandedSubBlockStart.gifContractedSubBlock.gif    onRowSelected: function(_sel, _index, _r)dot.gif{
277InBlock.gif        this.fireEvent("rowselect", _r);//发布事件
278ExpandedSubBlockEnd.gif    }

279InBlock.gif    
280ExpandedBlockEnd.gif}
)
281 None.gif
282 None.gif
283 None.gif
284 None.gif
285 None.gif

转载于:https://www.cnblogs.com/lxJack/archive/2011/03/24/1993489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值