Ext架构分析(4)--Container之旅

 BoxComponent继承了Component,主要是实现了设置组件的宽度、高度以及位置(相对于容器或相对于document.body),他的实现较为简单,需要注意的是:
    1.BoxComponent可以通过resizeEl属性设置进行调整大小的对象,positionEl属性设置调整位置的对象,并且在render事件中进行设置,将属性封装为Ext.element对象;
    2.setSize和setPosition方法是在afterRender事件中被触发的,换句话说,组件调整位置和大小是在渲染后进行的。 
ExpandedBlockStart.gif ContractedBlock.gif onRender :  function (ct, position) dot.gif
InBlock.gifExt.BoxComponent.superclass.onRender.call(
this, ct, position); 
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.resizeEl)dot.gif
InBlock.gif  
this.resizeEl = Ext.get(this.resizeEl); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.positionEl)dot.gif
InBlock.gif  
this.positionEl = Ext.get(this.positionEl); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gifafterRender : 
function () dot.gif
InBlock.gifExt.BoxComponent.superclass.afterRender.call(
this); 
InBlock.gif
this.boxReady = true
InBlock.gif
this.setSize(this.width, this.height); 
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.x || this.y)dot.gif
InBlock.gif  
this.setPosition(this.x, this.y); 
ExpandedSubBlockStart.gifContractedSubBlock.gif}
else if(this.pageX || this.pageY)dot.gif
InBlock.gif  
this.setPagePosition(this.pageX, this.pageY); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif

  Ext.Containr继承了BoxComponent,在他的initComponent方法中,增加了对以下事件的支持: 'afterlayout','beforeadd','beforeremove','add','remove'。
Container主要实现了对layout和items的管理。

  首先,让我们看一下Container对于items的管理:
  你可能会发现大部分的Widget都支持在构建器中传入一个items数组以非常方便的形式构建该Widget的子组件,而该数组大部分情况是由json构成,让我们看个例子:
ExpandedBlockStart.gif ContractedBlock.gif new  Ext.menu.Menu( dot.gif
InBlock.gifid: 
'mainMenu'
InBlock.gifitems: [ 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giftext: 
'I like Ext'
InBlock.gifchecked: 
true,   // when checked has a boolean value, it is assumed to be a CheckItem 
InBlock.gif
checkHandler: onItemCheck 
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giftext: 
'Ext for jQuery'
InBlock.gifchecked: 
true
InBlock.gifcheckHandler: onItemCheck 
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giftext: 
'I donated!'
InBlock.gifchecked:
false
InBlock.gifcheckHandler: onItemCheck 
ExpandedBlockEnd.gif}
'-'dot.gif 

None.gif

那么,这些json对象看不到表示任何对象类型的属性(xtype),Widget是怎样正确解析这些json对象的呢? 魔术就发生在Container中,首先,在Container的构建器中,有如下的语句:
None.gif var  items  =   this .items; // 如果传递了items对象 
ExpandedBlockStart.gifContractedBlock.gif
if (items) dot.gif
InBlock.gif
delete this.items; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(items instanceof Array)dot.gif{//items对象可以是数组,也许这样写更清楚些:this.add(items) 
InBlock.gif
this.add.apply(this, items); 
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif
InBlock.gif
this.add(items); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif

实际上,大多Widget都有自己的缺省的add的实现以满足自身的要求,Container也提供了一个缺省的add方法的实现如下:
ExpandedBlockStart.gif ContractedBlock.gif add :  function (comp) dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(!this.items)dot.gif{//如果未实现items数组,创建items数组 
InBlock.gif
this.initItems(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
var a = arguments, len = a.length;//如果传入的是数组则对每个元素进行递归调用add方法 
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(len > 1)dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(var i = 0; i < len; i++dot.gif
InBlock.gif
this.add(a); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
return
ExpandedSubBlockEnd.gif}
 
InBlock.gif
//this.applyDefaults(comp)方法对元素设置了缺省属性,注意到此时为止,还没有生成相应的组件,现在的item对象依然还是一个简单的json对象。lookupComponent方法则会生成元素组件 
InBlock.gif
var c = this.lookupComponent(this.applyDefaults(comp));   
InBlock.gif
var pos = this.items.length; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.fireEvent('beforeadd'this, c, pos) !== false && this.onBeforeAdd(c) !== false)dot.gif
InBlock.gif
this.items.add(c); 
InBlock.gif
//把每个子元素的ownerCt设置成Container自己 
InBlock.gif
c.ownerCt = this
InBlock.gif
//触发add事件 
InBlock.gif
this.fireEvent('add'this, c, pos); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
return c; 
ExpandedBlockEnd.gif}

None.gif

让我们看一下lookupComponent方法的实现:
ExpandedBlockStart.gif ContractedBlock.gif lookupComponent :  function (comp) dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(typeof comp == 'string')dot.gif
InBlock.gif
//如果传入的是字符串,进行查找 
InBlock.gif
return Ext.ComponentMgr.get(comp); 
ExpandedSubBlockStart.gifContractedSubBlock.gif}
else if(!comp.events)dot.gif
InBlock.gif
//如果是对象,但不是继承自Observable的对象(在这里,即不是Widget组件对象),则新建一个对象,这就是我们前面讨论的情况,传入的是配置数组。 
InBlock.gif
return this.createComponent(comp); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
return comp; 
ExpandedBlockEnd.gif}

魔术的答案在这里,createComponent 方法的实现:
ExpandedBlockStart.gif ContractedBlock.gif createComponent :  function (config) dot.gif
InBlock.gif
//this.defaultType是"panel",Container缺省实现是根据传入的json对象创建相应的panel 
InBlock.gif
return Ext.ComponentMgr.create(config, this.defaultType); 
ExpandedBlockEnd.gif}

而ComponentMgr的create方法的实现也很简单:
ExpandedBlockStart.gif ContractedBlock.gif create :  function (config, defaultType) dot.gif
InBlock.gif
return new types[config.xtype || defaultType](config); 
ExpandedBlockEnd.gif}
 

最终,秘密揭晓,Container的缺省实现将根据传入的items数组中的每个item的xtype属性进行子元素的创建。如果在item中未指定xtype,则根据配置创建panel.

Ext.Container除了通过add()方法,还提供了insert(),remove()等方法实现了对items的维护。在item中的每个元素被加入items之前,都调用beforeAdd方法,如果返回值为true,则该元素元素被设置缺省属性(通过applyDefaults方法),并吧ownerCt属性赋为container,然后加入items,并触发add事件。


Container还提供了两个很有用的方法:bubble和cascade。
bubble方法实现了一个方法在父容器中的递归调用,当然,只要方法在任何一个父容器中返回false,则调用被终止;
cascade方法则实现了方法在容器的子元素中被调用;
需要指出的是,如果未设置任何layout,则container返回ContainerLayout:
ExpandedBlockStart.gif ContractedBlock.gif getLayout :  function () dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(!this.layout)dot.gif
InBlock.gif
var layout = new Ext.layout.ContainerLayout(this.layoutConfig); 
InBlock.gif
this.setLayout(layout); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
return this.layout; 
ExpandedBlockEnd.gif}
 
None.gif


让我们再看一下对于layout的管理,通过render方法,Container设置了layout对象并调用了doLayout方法:
ExpandedBlockStart.gif ContractedBlock.gif render :  function () dot.gif
InBlock.gif    Ext.Container.superclass.render.apply(
this, arguments); 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(this.layout)dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(typeof this.layout == 'string')dot.gif
InBlock.gif        
this.layout = new Ext.Container.LAYOUTS[this.layout.toLowerCase()](this.layoutConfig); 
ExpandedSubBlockEnd.gif      }
 
InBlock.gif      
this.setLayout(this.layout); 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(this.activeItem !== undefined)dot.gif
InBlock.gif          
var item = this.activeItem; 
InBlock.gif          
delete this.activeItem; 
InBlock.gif          
this.layout.setActiveItem(item); 
InBlock.gif          
return
ExpandedSubBlockEnd.gif      }
 
ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(!this.ownerCt)dot.gif
InBlock.gif      
this.doLayout(); 
ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(this.monitorResize === true)dot.gif
InBlock.gif      Ext.EventManager.onWindowResize(
this.doLayout, this); 
ExpandedSubBlockEnd.gif  }
 
ExpandedBlockEnd.gif}
 
None.gif
doLayout方法则调用自己的layout对象的layout方法并遍历items中的元素,逐个调用layout方法:
ExpandedBlockStart.gif ContractedBlock.gif if ( this .rendered  &&   this .layout) dot.gif
InBlock.gif    
this.layout.layout(); 
ExpandedBlockEnd.gif}
 
ExpandedBlockStart.gifContractedBlock.gif
if ( this .items) dot.gif
InBlock.gif    
var cs = this.items.items; 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for(var i = 0, len = cs.length; i < len; i++dot.gif
InBlock.gif      
var c = cs; 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(c.doLayout)dot.gif
InBlock.gif          c.doLayout(); 
ExpandedSubBlockEnd.gif      }
 
ExpandedSubBlockEnd.gif  }
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/meetrice/archive/2008/05/23/1206117.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值