學(xué)習(xí)YUI.Ext 第四天--對話框Dialog的使用
更新時間:2007年03月10日 00:00:00 作者:
使用方法:
1.加入YUI.Ext 庫到你的web程序:
<!-- YAHOO UI Utilities Lib, you will need to replace this with the path to your YUI lib file -->
<script type="text/javascript" src="deepcms/yui/utilities_2.1.0.js"></script><script type="text/javascript" src="deepcms/yui-ext.0.33-rc1/yui-ext.js"></script>
2.加入樣式表 CSS Style 。如果你是一個美工,最多打交道的地方,可能就是這幾個文件:
<!--YahooUI! Ext -->
<link rel="stylesheet" type="text/css" href="yui-ext.0.33-rc1/resources/css/reset-min.css" />
<link rel="stylesheet" type="text/css" href="yui-ext.0.33-rc1/resources/css/resizable.css" />
<link rel="stylesheet" type="text/css" href="yui-ext.0.33-rc1/resources/css/tabs.css" />
<link rel="stylesheet" type="text/css" href="yui-ext.0.33-rc1/resources/css/basic-dialog.css" />
3.加入一個holder.holder的意思是一個載體,JS處理好數(shù)據(jù),轉(zhuǎn)變成內(nèi)容(Contents,文字、圖片、表格等)放在這里,也可以理解為一個架子,承托所有內(nèi)容。holder表現(xiàn)形式很簡單,通常是幾行div。
<div id="hello-dlg" style="visibility:hidden;position:absolute;top:0px;">
<div class="ydlg-hd">中易旅游網(wǎng)</div>
<div class="ydlg-bd"> 您沒確認(rèn)條款內(nèi)容。</div>
</div>
4.加入定義Dialog腳本,實(shí)例化Dialog:
// create the HelloWorld application (single instance)
var HelloWorld = function(){
// everything in this space is private and only accessible in the HelloWorld block
//任何在這個區(qū)域的都是私有變量 ,只能在HelloWorld訪問
var dialog, showBtn;
var toggleTheme = function(){
getEl(document.body, true).toggleClass('ytheme-gray');
};
// return a public interface
return {
init : function(){
showBtn = getEl('goNextBtn'); //綁定一個按鈕
// attach to click event 加入事件
/showBtn.on('click', this.showDialog, this, true);
///getEl('theme-btn').on('click', toggleTheme);
},
showDialog : function(){
if(!dialog){ //因?yàn)椴捎脝卫J?,不能被new重復(fù)實(shí)例。這里是用懶惰的方法作判斷。
dialog = new YAHOO.ext.BasicDialog("hello-dlg", {
modal:true,//這段代碼是dialog的一些參數(shù),如大小、有冇陰影、是否覆蓋select等
autoTabs:false,
width:180,
height:100,
shadow:true,
minWidth:508,
shim: true,
autoScroll: false,
resizable:false,
minHeight:300
});
dialog.addKeyListener(27, dialog.hide, dialog);//鍵盤事件Esc退出
dialog.addButton('退出', dialog.hide, dialog);
}
dialog.show(showBtn.dom);//參數(shù)為動畫效果出現(xiàn)的地方
}
};
}();//注意這對括號,如果沒有將不會執(zhí)行。
//用onDocumentReady代替windows.onload初始化程序。當(dāng)DOM準(zhǔn)備好,無須等待載入圖片和其他資源;有關(guān)兩者的討論,請看這里
YAHOO.ext.EventManager.onDiocumentReady(HelloWorld.init, HelloWorld, true);
難點(diǎn)分析: Singleton Pattern 設(shè)計(jì)模式之單例
什么是 Singleton Pattern?
Anwser: 單例模式(Singleton Pattern)是一種非常基本和重要的創(chuàng)建型模式。 “單例”的職責(zé)是保證一個類有且只有一個實(shí)例,并提供一個訪問它的全局訪問點(diǎn)。 在程序設(shè)計(jì)過程中,有很多情況下需要確保一個類只能有一個實(shí)例。
單例模式有什么好處?
Anwser: 1.減少new操作帶來的內(nèi)存占用;2.在JS中,可以建立你自己的命名空間namespace.
如何實(shí)現(xiàn)單例模式?
Anwser:
傳統(tǒng)的編程語言中為了使一個類只有一個實(shí)例,最容易的方法是在類中嵌入靜態(tài)變量,并在第一個實(shí)例中設(shè)置該變量,而且每次進(jìn)入構(gòu)造函數(shù)都要做檢查,不管類有多少個實(shí)例,靜態(tài)變量只能有一個實(shí)例。為了防止類被多次初始化,要把構(gòu)造函數(shù)聲明為私有的,這樣只能在靜態(tài)方法里創(chuàng)建一個實(shí)例。 請看下面的例子:
復(fù)制代碼 代碼如下:
function __typeof__(objClass) //返回自定義類的名稱
{
if ( objClass != undefined && objClass.constructor )
{
var strFun = objClass.constructor.toString();
var className = strFun.substr(0, strFun.indexOf('('));
className = className.replace('function', '');
return className.replace(/(^\s*)|(\s*$)/ig, '');
}
return typeof(objClass);
}
function Singleton()
{
// template code for singleton class.靜態(tài)屬性判斷是否重復(fù)生產(chǎn)new對象
if ( this.constructor.instance )
{
return this.constructor.instance;
}
else{ alert("else");this.constructor.instance = this;
}
this.value = parseInt(Math.random()*1000000);
this.toString = function(){ return '[class Singleton]'; }
}
Singleton.prototype.GetValue = function(){return this.value; };
Singleton.prototype.SetValue = function(value){ this.value = value; };
var singleton = new Singleton();
alert(__typeof__(singleton));
alert(singleton.GetValue());
alert(singleton.GetValue());
singleton.SetValue(1000000);
var singleton = new Singleton();
alert(singleton.GetValue());
alert(singleton.GetValue());
第二個和第三個是random出來的。總之有兩組結(jié)果是一樣的??梢钥闯鯯ingleton的模式下,對象實(shí)例化一次后,其屬性或變量不會變化(哪怕是new的操作),除非你人為地修改。 上面的例子通過調(diào)用Constructor靜態(tài)屬性來獲得對象確實(shí)可以保證“唯一實(shí)例”,然而,這個例子的失敗之處在于它并沒有有效地禁止Singleton對象的構(gòu)造,因此如果我們在程序代碼中人工加入new Singleton (),仍然可以獲得到多個對象而導(dǎo)致模式失敗。因此要完全實(shí)現(xiàn)Singleton并沒有想象中那么簡單。 于是我們進(jìn)一步思考,得到了下面第三種方法,這種方法巧妙利用了“匿名”函數(shù)的特征來禁止對SingletonObject類構(gòu)造函數(shù)的訪問,可以說比較好的模擬了私有構(gòu)造函數(shù)的特性,從而比較完美地解決了用javascript實(shí)現(xiàn)Singleton Pattern的問題。
復(fù)制代碼 代碼如下:
(function(){
//instance declared
//SingletonFactory Interface
SingletonFactory = {getInstance : getInstance}
//private classes
function SingletonObject(){
SingletonObject.prototype.methodA = function() {alert('methodA');}
SingletonObject.prototype.methodB = function() { alert('methodB'); }
SingletonObject.instance = this;
}
//SingletonFactory implementions
function getInstance(){
if(SingletonObject.instance == null) return new SingletonObject();
else return SingletonObject.instance;
}
})();
var instA = null;
try
{
alert("試圖通過new SingletonObject()構(gòu)造實(shí)例!");
instA = new SingletonObject();
}
catch(e){alert("SingletonObject構(gòu)造函數(shù)不能從外部訪問,系統(tǒng)拋出了異常!");}
instA = SingletonFactory.getInstance(); //通過Factory上定義的靜態(tài)方法獲得
var instB = SingletonFactory.getInstance();
instA.methodA();
instB.methodA();
alert(instA == instB); //成功
相關(guān)文章
Ext第一周 史上最強(qiáng)學(xué)習(xí)筆記---GridPanel(基礎(chǔ)篇)
我不想教各位新手什么高級技術(shù),因?yàn)槲乙苍谘芯?,只是想教大家一個思考的方向,能夠具有舉一反三的能力,能夠真正學(xué)會Ext和開始深入了解。2008-12-12
今天頭兒分了一個項(xiàng)目的一張頁面給我做!要修改幾個問題!一個按F5局部刷新Grid網(wǎng)格中數(shù)據(jù)!第二個:網(wǎng)格一些信息是雙擊打開就已經(jīng)讀?。?/div> 2010-09-09
解決extjs在firefox中關(guān)閉窗口再打開后iframe中js函數(shù)訪問不到的問題
最近做ext時遇到一個問題,在firefox中瀏覽ext應(yīng)用,加載后第一次打開一個嵌入iframe的Window時,可以直接通過js代碼來執(zhí)行 iframe中的js函數(shù),但是如果將窗口關(guān)閉后重新再打開,將會拋出異常,說是funcName is not a function2008-11-11
Ext面向?qū)ο箝_發(fā)實(shí)踐(續(xù))
我的上一篇文章《Ext面向?qū)ο箝_發(fā)實(shí)踐》中簡述了如何編寫一個面向?qū)ο蟮臄?shù)據(jù)維護(hù)小程序,但這一些都是基于一個客戶端數(shù)據(jù),即用戶一旦刷新頁面,所有的操作都將丟失,現(xiàn)在我們就接著上一篇文章來繼續(xù)講一下如何對數(shù)據(jù)表進(jìn)行增、刪、改、查操作。2008-11-11最新評論

