`

Ext.apply 在自定义Ext类时的使用

    博客分类:
  • Ext
阅读更多
Ext.applyIf(object,config);

Ext.apply(object,config)

 

  两个函数都是将config的属性拷贝到object中,但是如果object已有属性,Ext.applyIf(object,config);是不会覆盖原有属性的。

 

 

 注意:传入参数如果是undefined,config中的属性是不会拷贝到config中的。

 

 定义一个Ext 组件类,通常定义一些默认的属性,传入新的属性覆盖原有的属性。

 以前我们采用Ext.apply(this,config);但是不能保证Ext采用传入的参数构造子类。

 

所以在定义Ext类的时候,可以使用Ext.apply,将传入参数覆盖到原有默认参数中。

Ext.namespace('com.resoft.performance');
/**
 * 图表弹出窗口
 *
 * @param {}
 *            config
 */
com.resoft.performance.ChartWindow = function(config) {

	com.resoft.performance.ChartWindow.superclass.constructor.call(this, Ext
					.applyIf(config, {
								maximizable : true,
								width : 200,
								height : 100,
								modal : true,
								collapsible : true,
								renderTo : Ext.getBody(),
								layout : 'border',
								items : {
									region : 'center',
									iconCls : 'panel-grid-icon',
									title : '查询结果',
									autoScroll : true,
									margins : '0 5 0 5',
									layout : 'fit',
									items : new Ext.TabPanel({
												region : 'center',
												margins : '3 3 3 0',
												activeTab : 0,
												defaults : {
													autoScroll : true
												},
												items : [{
															title : 'Bogus Tab',
															html : 'ffff'
														}, {
															title : 'Another Tab',
															html : 'ffff'
														}, {
															title : 'Closable Tab',
															html : 'efeafegnwrth',
															closable : true
														}]
											})
								}
							}));

}

Ext.extend(com.resoft.performance.ChartWindow, Ext.Window, {
			width : 600,
			height : 400,
			productType : 'gg',
			chartType : 'f'
		})

 

但是如果调用代码构造时,没有任何参数传入,构造时的参数将为空,即Ext.apply(object,config),如果object为undefined, 返回也为undefined。如下代码就不会显示任何item。

 

Ext.onReady(function() {
   var s = new com.resoft.performance.ChartWindow();
   s.show();
  });

 

所以可以采用下列代码进行替换

Ext.apply( {.....},config)

 这样默认的属性就会强制被config覆盖,如果config为空,也会按照默认的属性进行构造

 

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics