`

renderTo和applyTo的区别

阅读更多

Extjs的组件有两个看起来类似的配置项,applyTo和renderTo,这两个配置项都是用来指定将该extjs组件加载到什么位置。那他们到底有什么区别呢?

个人认为这两篇文章写的不够通俗。

写一个简单的例子来看看最终生成了什么代码。

<head> 
<title>RenderTo and ApplyTo</title> 
<link rel="Stylesheet" type="text/css" href="ext-3.1.0/resources/css/ext-all.css" /> 
<script type="text/javascript" src="ext-3.1.0/adapter/ext/ext-base-debug.js"></script> 
<script type="text/javascript" src="ext-3.1.0/ext-all-debug.js"></script> 
<script type="text/javascript" src="ext-3.1.0/src/locale/ext-lang-zh_CN.js"></script> 
<script type="text/javascript"> 
Ext.onReady(function() { 
var button = new Ext.Button({ 
renderTo: 'button', 
text:'OK' 
}); 

}); 
</script> 
</head> 
<body> 
<div id="button">sadfa</div> 
</body> 
</html> 

 此代码生成的html如下:

 

 

 如果是applyTo:button,则生成的代码为:


 很明显,简单的说,applyTo是将组件加在了指定元素之后,而renderTo则是加在指定元素之内。

 

接下来,我们再稍稍探寻下extjs源码的奥秘。看看extjs内部是如何使用这两个配置项的,利用firebug插件调试一下ext-all-debug.js这个文件。在Ext.Component的构造函数Ext.Component = function(config){…}中有这样一段代码(3.1.0版本是9270行):

if(this.applyTo){ 
this.applyToMarkup(this.applyTo); 
delete this.applyTo; 
}else if(this.renderTo){ 
this.render(this.renderTo); 
delete this.renderTo; 
} 

 可见applyTo属性使得Component调用applyToMarkup方法,而renderTo使得它调用render方法,并且如果两个都设置的话仅有applyTo有效,这点在extjs的文档中也有特别指出。
appylToMarkup方法如下(3.1.0版本是9560行)

applyToMarkup : function(el){ 
this.allowDomMove = false; 
this.el = Ext.get(el); 
this.render(this.el.dom.parentNode); 
} 

 

它最终调用的也是render,不过render的位置是parentNode,render方法如下(3.1.0版本是9370行) 

render : function(container, position){ 
if(!this.rendered && this.fireEvent('beforerender', this) !== false){ 
if(!container && this.el){ 
this.el = Ext.get(this.el); 
container = this.el.dom.parentNode; 
this.allowDomMove = false; 
} 
this.container = Ext.get(container); 
if(this.ctCls){ 
this.container.addClass(this.ctCls); 
} 
this.rendered = true; 
if(position !== undefined){ 
if(Ext.isNumber(position)){ 
position = this.container.dom.childNodes[position]; 
}else{ 
position = Ext.getDom(position); 
} 
} 
this.onRender(this.container, position || null); 
if(this.autoShow){ 
this.el.removeClass(['x-hidden','x-hide-' + this.hideMode]); 
} 
if(this.cls){ 
this.el.addClass(this.cls); 
delete this.cls; 
} 
if(this.style){ 
this.el.applyStyles(this.style); 
delete this.style; 
} 
if(this.overCls){ 
this.el.addClassOnOver(this.overCls); 
} 
this.fireEvent('render', this); 

var contentTarget = this.getContentTarget(); 
if (this.html){ 
contentTarget.update(Ext.DomHelper.markup(this.html)); 
delete this.html; 
} 
if (this.contentEl){ 
var ce = Ext.getDom(this.contentEl); 
Ext.fly(ce).removeClass(['x-hidden', 'x-hide-display']); 
contentTarget.appendChild(ce); 
} 
if (this.tpl) { 
if (!this.tpl.compile) { 
this.tpl = new Ext.XTemplate(this.tpl); 
} 
if (this.data) { 
this.tpl[this.tplWriteMode](contentTarget, this.data); 
delete this.data; 
} 
} 
this.afterRender(this.container); 
if(this.hidden){ 
this.doHide(); 
} 
if(this.disabled){ 
this.disable(true); 
} 
if(this.stateful !== false){ 
this.initStateEvents(); 
} 
this.fireEvent('afterrender', this); 
} 
return this; 
} 

 

render的方法看起来相当的复杂,仔细阅读也能理解个大概,主要是为一个DOM节点设置class,可见性,在onRender方法中生成相应的HTML代码。

 

个人认为:el是紧紧包裹EXT组件的一个DOM节点,一般是由EXT自己生成,好像细胞膜一样,如果拨开了它,那么整个组件就显示的不完整,很可能显示的不正常。而render中的container(也就是applyTo的父原始,renderTo中的指定元素),是该组件的父元素,这个container可以是HTML代码,亦或是EXT组件。

综上所述:applyTo和renderTo没有本质上的区别,只是renderer的位置不同。


 

 

 

分享到:
评论
1 楼 xiaohui886688 2011-01-23  
不错,分析得挺不错。  

相关推荐

    Extjs学习笔记之五 一个小细节renderTo和applyTo的区别

    ExtJS中的renderTo和applyTo的差别 对applyTo和renderTo的理解和思考个人认为这两篇文章写的不够通俗。写一个简单的例子来看看最终生成了什么代码, 代码如下: &lt;head&gt; &lt;title&gt;RenderTo and ApplyTo&lt;/...

    Final render sp3 for 3dsmax2010英文版

    4. Launch IP-Clamp.exe and choose 'localhost' as primary network adapter, apply. 4. Create a request file using your personnal informations and a licence code generated by our keygen. 5. Use keygen...

    R Graphics Cookbook

    Each recipe tackles a specific problem with a solution you can apply to your own project, and includes a discussion of how and why the recipe works. Most of the recipes use the ggplot2 package, a ...

    Mastering JavaServer Faces

    The authors then explain how to apply JSF, including how to integrate JSF user interfaces with the Business Tier and how to render your own user interface components. By following this approach, you'...

    Learning.Information.HTML5.Mobile.for.Android.and.iOS.epub

    directed learners who are comfortable with HTML/Javascript and who want to learn how to create mobile applications using HTML5 for Android and iOS. Readers will learn how to use CSS3 and HTML5 Canvas to render 2D shapes...

    英文原版-Hybrid Animation Integrating 2D and 3D Assets 2nd Edition

    By the end of the book you’ll be able to see how to apply these techniques to the software you have now.Insight and inspiration are at your fingertips with exercises, step-by-step tutorials and ...

    Learning Three.js: The Javascript 3D Library for WebGL

    Create and animate stunning 3D graphics using the open source Three.js JavaScript library Overview Create and animate beautiful ...Apply physics to your 3D objects using the Physijs JavaScript library

    WPTools.v6.29.1.Pro

    We recommend to use TMS components to create modern user interfaces, toolbars and ribbons. 19.2.2013 - WPTools 6.29.1 - fix in rtf writing code to solve problem with merged cells - fix possible range...

    ImageMagick图片批量处理

    -direction type render text right-to-left or left-to-right -display server get image or font from this X server -dispose method layer disposal method -dither method apply error diffusion to image ...

    GPU-BASED-TECHNIQUES-FOR-GLOBAL-ILLUMINATION-EFFECTS

    This book presents techniques to render photo-realistic images by programming the Graphics Processing Unit (GPU). We discuss effects such as mirror reflections, refractions, caustics, diffuse or ...

    GPU-BASED TECHNIQUES FOR GLOBAL ILLUMINATION EFFECTS(Part1)

    This book presents techniques to render photo-realistic images by programming the Graphics Processing Unit (GPU). We discuss effects such as mirror reflections, refractions, caustics, diffuse or ...

    cuteEditor6.0

    更快的下载和加载网页: Less HTML results in smaller file sizes and quicker downloads. Modern web browsers render pages faster when they are in their standards mode than when they are in their ...

    SFML.Game.Development.by.Example.178528734

    The book starts by building a clone of the classical snake game where you will learn how to open a window and render a basic sprite, write well-structured code to implement the design of the game, and...

    .Learning.iPhone.Game.Development.with.Cocos2D.3.0

    Use the physics engine to apply forces and detect collisions Add sounds and music and change playback properties Integrate your game with Game Center and make In-App purchases Approach This book is an...

    C# Game Programming Cookbook for Unity 3D - 2014

    1.1.5 Where to Now?.............................................8 2. Building the Core Game Framework 9 2.1 Controllers and Managers............................................11 2.1.1 Controllers.......

    大数据可视化(四)比例数据可视化.pdf

    :, 0].apply(str).values.tolist() print(datax) y1=data["Under 5"].values.tolist() y2=data["5 to 19"].values.tolist() y3=data["20 to 44"].values.tolist() y4=data["45 to 64"].values.tolist() y5=data["65+...

    DWGdirectX2.7.3

    Apply a transformation to an entity. Modify arbitrary properties of database objects. Many more. Export DWG/DXF data to SVG, PDF, DWF, BMP. Import DWF files. Full support for "round-trip" data. ...

    button的js代码

    renderTo : 'docbody', menu: undefined, split: false, layoutConfig: { columns:3 }, constructor: function(desc, buttons){ // apply test configs for(var i = 0, b; b = buttons[i]; i++){ b.menu...

    unigui0.83.5.820

    - 0000791: UniDBGrid, UniStringGrid: Option to disable custom renderer to speed-up render time. 0.83.4.819 +---------------------------------------------------------------------------------------- -...

Global site tag (gtag.js) - Google Analytics