国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

Extjs學(xué)習(xí)筆記之五 一個(gè)小細(xì)節(jié)renderTo和applyTo的區(qū)別

2019-11-21 00:50:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

ExtJS中的renderTo和applyTo的差別

對(duì)applyTo和renderTo的理解和思考

個(gè)人認(rèn)為這兩篇文章寫(xiě)的不夠通俗。寫(xiě)一個(gè)簡(jiǎn)單的例子來(lái)看看最終生成了什么代碼,

復(fù)制代碼 代碼如下:

<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如下:
image 
如果是applyTo:button,則生成的代碼為:

image

很明顯,簡(jiǎn)單的說(shuō),applyTo是將組件加在了指定元素之后,而renderTo則是加在指定元素之內(nèi)

接下來(lái),我們?cè)偕陨蕴綄は耬xtjs源碼的奧秘。看看extjs內(nèi)部是如何使用這兩個(gè)配置項(xiàng)的,利用firebug插件調(diào)試一下ext-all-debug.js這個(gè)文件。

在Ext.Component的構(gòu)造函數(shù)Ext.Component = function(config){…}中有這樣一段代碼(3.1.0版本是9270行):

復(fù)制代碼 代碼如下:

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

可見(jiàn)applyTo屬性使得Component調(diào)用applyToMarkup方法,而renderTo使得它調(diào)用render方法,并且如果兩個(gè)都設(shè)置的話僅有applyTo有效,這點(diǎn)在extjs的文檔中也有特別指出。

appylToMarkup方法如下(3.1.0版本是9560行),
復(fù)制代碼 代碼如下:

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

它最終調(diào)用的也是render,不過(guò)render的位置是parentNode,render方法如下(3.1.0版本是9370行)
復(fù)制代碼 代碼如下:

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方法看起來(lái)比較復(fù)雜,仔細(xì)閱讀下其實(shí)也不是太難,主要就是為一個(gè)DOM節(jié)點(diǎn)設(shè)置class,可見(jiàn)性,在onRender方法中會(huì)對(duì)這個(gè)組件生成相應(yīng)的html代碼。
對(duì)applyTo和renderTo的理解和思考 中提到的el配置屬性,我查extjs的文檔發(fā)現(xiàn)這是一個(gè)只讀屬性,雖然有方法覆蓋它,不過(guò)一般不需要手動(dòng)設(shè)置,下面是Panel的公共屬性el的文檔原文:

el : Ext.Element

The Ext.Element which encapsulates this Component. Read-only.

This will usually be a <DIV> element created by the class's onRender method, but that may be overridden using the autoEl config.

Note: this element will not be available until this Component has been rendered.

所以我估計(jì)此文寫(xiě)的是以前版本的extjs。個(gè)人認(rèn)為,el是緊包裹著extjs組件的一個(gè)DOM節(jié)點(diǎn),一般是由extjs自己生成的,好像細(xì)胞膜一樣,如果撥開(kāi)了它,那么這個(gè)組件就不完整了,很可能會(huì)表現(xiàn)的不正常。而render方法中的container(也就是applyTo中指定元素的父元素,renderTo中指定的元素),是該組件的父元素,這個(gè)container中可以包括其他的html元素或者extjs組件。

綜上所述,其實(shí)applyTo和renderTo沒(méi)有很本質(zhì)區(qū)別,只是render的位置不同。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金沙县| 蒲城县| 瑞金市| 贵溪市| 大荔县| 视频| 六枝特区| 寿宁县| 新绛县| 西丰县| 略阳县| 高尔夫| 阜康市| 大化| 黄龙县| 泸溪县| 紫金县| 灌阳县| 北票市| 饶平县| 彩票| 平湖市| 东辽县| 岳阳市| 龙里县| 镇安县| 阿瓦提县| 井冈山市| 东兴市| 遵义县| 吴堡县| 桐梓县| 巧家县| 应用必备| 甘德县| 武夷山市| 宁河县| 临邑县| 旺苍县| 陈巴尔虎旗| 苏尼特右旗|