一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

dojo事件機(jī)制及壓縮

 tinaroad 2007-06-20
dojo的事件機(jī)制一
[不指定 2006/08/10 22:18 | by zkj ]
不同與一般web程序員口中的DOM事件,Dojo給瀏覽器上的事件提供更廣闊的意義.dojo.event.*可以讓開發(fā)者把任何函數(shù)引用作為事件的監(jiān)聽器.用Dojo,你可以用統(tǒng)一的API為任何動(dòng)作注冊(cè)響應(yīng)函數(shù).
這篇文章有以下幾個(gè)要點(diǎn):
·  how to use these tools
·  what makes them completely different from other JavaScript event systems you may have used
·  why you‘ll never start writing JavaScript without dojo.event.connect() again.
Examples

dojo.event.connect() 能幫助你簡(jiǎn)單的為DOM對(duì)象注冊(cè)時(shí)間.
var handlerNode = document.getElementById("handler");

function handleOnClick(evt){
   // ...
}

dojo.event.connect(handlerNode, "onclick", "handleOnClick");

Dojo.event.connect 修訂了IE討厭的與DOM標(biāo)準(zhǔn)沖突的事件模型(window.event).放心,在我們的處理函數(shù)handleOnClick函數(shù)的第一個(gè)參數(shù)是 event對(duì)象.并且修訂了event對(duì)象的一些標(biāo)準(zhǔn)方法如canceling event bubbling.詳細(xì)見dojo-0.3.1-ajax\src\event\browser.js 227行.
這里需要在click前加on.和prototype.js,YUI處理的方法不一樣!
如果我們不想定義太多的全局函數(shù),那javascript的閉包,匿名函數(shù)可以實(shí)現(xiàn):

var handlerNode = document.getElementById("handler");

dojo.event.connect(handlerNode, "onclick", function(evt){
   // ...
});

如果我們的事件處理函數(shù)的執(zhí)行環(huán)境輸入某個(gè)對(duì)象怎么辦呢?

可能你立刻會(huì)寫出以下代碼:

var handlerNode = document.getElementById("handler");

handlerNode.onclick = function(evt){
   object.handler(evt);
};

哦,如此復(fù)雜,用Dojo可以簡(jiǎn)單的實(shí)現(xiàn):
var handlerNode = document.getElementById("handler");

dojo.event.connect(handlerNode, "onclick", object, "handler");
connect() call 將確保當(dāng)handlerNode.onclick()調(diào)用時(shí), object.handler()將調(diào)用,并且參數(shù)的相同的.

Connect()也可以處理多個(gè)handle函數(shù),它們的調(diào)用順序與注冊(cè)順序一致.

var handlerNode = document.getElementById("handler");

dojo.event.connect(handlerNode, "onclick", object, "handler");
dojo.event.connect(handlerNode, "onclick", object, "handler2");

注意以上代碼當(dāng)點(diǎn)際node或調(diào)用object.handler(),都將會(huì)觸發(fā)object.handler2()執(zhí)行.
到現(xiàn)在connect() 可以接受以下類型的參數(shù):
·  object, name, name     dom  “onclick”  funtion name
·  object, name, function pointer   dom  “onclick”  funtion指針
·  object, name, object, name   dom  “onclick”  function object  funtion
以上是connect()函數(shù)處理DOM事件的方法.當(dāng)然文章開頭就說過,dojo可以處理更加廣闊的”事件”.請(qǐng)看下面簡(jiǎn)單的例子:

var exampleObj = {
   counter: 0,
   foo: function(){
       alert("foo");
       this.counter++;
   },
   bar: function(){
       alert("bar");
       this.counter++;
   }
};

我想當(dāng)exampleObj.foo() 被調(diào)用時(shí),exampleObj.bar() 被調(diào)用.前面DOM事件注冊(cè)時(shí)也會(huì)有這種效果.但如果exampleObj.foo()不是被dom時(shí)間觸發(fā),意義更大.
dojo.event.connect(exampleObj, "foo", exampleObj, "bar");
現(xiàn) 在調(diào)用 foo() 也會(huì)出發(fā)調(diào)用 bar(), 因此增加counter變量?jī)纱蝨wice,并且alerting "foo" 然后是 "bar". Bar()中可以應(yīng)用foo()函數(shù)的調(diào)用結(jié)果.如果你的代碼中應(yīng)用這個(gè)函數(shù)連接,特別是后一個(gè)函數(shù)用到前個(gè)函數(shù)的執(zhí)行結(jié)果時(shí),小心不要直接調(diào)用后一個(gè)函 數(shù).

Disconnection and Multi-Connection

當(dāng)然你可以注冊(cè)事件監(jiān)聽函數(shù),當(dāng)然也可以注銷.dojo.event.disconnect()會(huì)處理注銷.但要注意,connect,disconnect的參數(shù)必須嚴(yán)格一致才可以.
如果你不注意為某個(gè)dom對(duì)象,或函數(shù)注冊(cè)了兩遍監(jiān)聽函數(shù),當(dāng)出發(fā)事件時(shí),dojo會(huì)執(zhí)行兩遍函數(shù)。No,no怎么辦呢?別急dojo為我們做了相對(duì)不錯(cuò)的解決方案。
Connecting Once and Using Keywords
其實(shí)connect()函數(shù)提供一個(gè)參數(shù)來判斷是否只運(yùn)行一次,不過太惡心了,是第八個(gè)參數(shù)

ao.srcObj = args[1];
ao.srcFunc = args[2];
ao.adviceObj = args[3]
ao.adviceFunc = args[4];
ao.aroundObj = args[5];
ao.aroundFunc = args[6];
ao.once = args[7];
ao.delay = args[8];
ao.rate = args[9];
ao.adviceMsg = args[10];

如果你用這個(gè)方法,那么你可能必須熟悉dojo的一些高級(jí)功能,或者了解AOP,這是不能讓我們?nèi)淌艿摹?br>幸好,dojo提供了相對(duì)簡(jiǎn)單的解決辦法。用kwConnect():
注意參數(shù)是一個(gè)對(duì)象,其實(shí)這個(gè)對(duì)象就是dojo內(nèi)部創(chuàng)建對(duì)象的一部分。

dojo.event.kwConnect({
   srcObj:     exampleObj,
   srcFunc:    "foo",
   targetObj:  exampleObj,
   targetFunc: "bar",
   once:       true
});

當(dāng)然dojo提供的對(duì)應(yīng)的 kwDisconnect method.
Delaying Execution
There‘s one more modifier up the sleeve of connect()/kwConnect(); delayed calling. The delay property in kwConnect (the 9th positional parameter for connect) is a delay in milliseconds for those platforms that support it (all browsers do).
Dojo對(duì)于事件處理還有個(gè)延遲執(zhí)行的功能。是dojo.event.connect()的第9個(gè)參數(shù),或者用dojo.event.kwConnect方法:如下,bar函數(shù)將在foo執(zhí)行三秒后執(zhí)行。

dojo.event.kwConnect({
   srcObj:     exampleObj,
   srcFunc:    "foo",
   targetObj:  exampleObj,
   targetFunc: "bar",
   once:       true ,
 delay:3000
});

exampleObj.foo();

dojo的事件太強(qiáng)大了,如果這樣注冊(cè)來注冊(cè)去的很容易造成循環(huán)connections.
A B C三個(gè)事件相互監(jiān)聽,造成運(yùn)行循環(huán)。這真是個(gè)麻煩事情,有個(gè)好消息是,如果有這個(gè)問題,你肯定過不了調(diào)試關(guān),瀏覽器會(huì)立刻報(bào)告這個(gè)異常的。
當(dāng)然你也可以用debug工具來檢查這個(gè)錯(cuò)誤  例如 Venkman.
[不指定 2006/08/10 20:54 | by zkj ]
http:///docs/compressor_system.html


在Dojo的build過程最后一個(gè)可選步驟是compression(壓縮).像其它JavaScript工具一樣,Dojo也用工具減少代碼量,可以被瀏覽器更快的下載.
   當(dāng)然,太多的壓縮工具存在,但各方面因素決定Dojo的壓縮工具是唯一的.
Example
為了運(yùn)行以下的例子,推薦你安裝jdk1.4.從Dojo官網(wǎng)下載壓縮工具.
http:///svn...
如果你看了前文,D:\zkj\dojosource\buildscripts\lib下有這個(gè)包

然后編輯infile.js文件,內(nèi)容如下:

function MyClass(){
   this.foo = function(argument1, argument2){
       var addedArgs = parseInt(argument1)+parseInt(argument2);
       return addedArgs;
   }

   var anonymousInnerFunction = function(){
       // do stuff here!
   }
}

function MyFunc(){
   // this is a top-level function
}







// 你可以輸入一些空格

我們?cè)诿钚休斎胍韵旅畹玫?outfile.js文件


java -jar custom_rhino.jar -c infile.js > outfile.js 2>&1
outfile.js的內(nèi)容是以下代碼.

function MyClass(){
this.foo=function(_1,_2){
var _3=parseInt(_1)+parseInt(_2);
return _3;
};
var _4=function(){
};
}
function MyFunc(){
}

你可以比較一下文件大小.
obelisk:/tmp/compress alex$ ls -lah
...
-rw-r--r--    1 alex  users  321B Aug 12 09:21 infile.js
-rw-r--r--    1 alex  users  140B Aug 12 09:21 outfile.js
321 bytes to 140 bytes, a 56% reduction. Not bad!

我在window操作系統(tǒng),但看代碼也知道壓縮不少.

Riding a Rhino

   這些工作是怎么完成的呢?為什么選擇這個(gè)工具而不是其它呢?Dojo的compressor基礎(chǔ)是Rhino. Rhino是來自the Mozilla project的javascript解釋引擎(java實(shí)現(xiàn)).而其它一些壓縮工具一般是自定義的規(guī)則表達(dá)式來完成壓縮的.
   相比那些regular-expression為基礎(chǔ)的工具,通過Rhino對(duì)js代碼的解析,Dojo compressor能得到更好的效果,體現(xiàn)在變量的上下文環(huán)境,變量名等.Dojo compressor不會(huì)改變代碼的public API.

API Safety

There are many "obfuscators" available in addition to size reduction tools. Over the years, many people have attempted to "encrypt" or otherwise obfuscate JavaScript sent over the wire to browsers, and it never pans out. Why not? For starters, JavaScript (as implemented in browsers) is completely interpreted. This means that any further compilation beyond source transformations will not work everywhere, and the tool provides a "decryption" tool along with the "encrypted" or obfuscated source, the unencrypted version will be available at runtime for anyone with a debugger to see. For those tools that just transform source code by mangling variable names, it‘s even easier to revert their changes. Therefore, obfuscation and encryption aren‘t useful goals. Size reduction, on the other hand, is a useful goal.
But not if your size-reduction tool breaks things. There are, of course, many increments available for the "compression" process. Potential choices available to a tool author include:
·  removing comments
·  collapsing line-beginning whitespace
·  removing line-ending whitespace
·  collapsing multiple blank lines
·  removing all new-line characters
·  removing whitespace around operators
·  removing whitespace near/around curly braces
·  replacing symbols with shorter names (this is how most "obfuscation" is done)
And the list goes on and gets ever-more esoteric as one tries to squeeze every last K out of a JavaScript file. But at some point, you can go too far. The Dojo compressor attempts to strike a balance between debuggability (not replacing all symbols and not removing all newlines) and size reduction.
Getting The Source
Rhino的源代碼可以從Mozilla CVS服務(wù)器得到:
http://www.mozilla.org/cvs...
具體路徑在:
/cvsroot/mozilla/js/rhino
Our patches should apply cleanly against Rhino HEAD and are available in unified diff format from:
http:///svn...
Unlike the rest of Dojo, the Dojo Foundation does not control the copyright of the original work, and we therefore cannot license this code under there AFL. It is made available under the tri-licensing terms of the Mozilla project.
The Future
The Dojo compression tool is by no means the last word in file-size or on-the-wire reduction. Gzipping content on the wire is the next obvious improvement for those deploying applications.
The Dojo package system and JSAN allow developers to include just those packages from a library that they require, and future work on real JS linkers will further strip down capable libraries like Dojo to only absolutely what is needed by application authors.
The Dojo project intends to continue to produce the best Open Source tools for JS and webapp developers, and we will make these transparently available in the Dojo build system, as we do today with the compression and package systems.
About The Author
Alex Russell is the project lead for Dojo and can be reached at . His blog is at: http://jotoolkit.or...

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    九九蜜桃视频香蕉视频| 午夜精品国产一区在线观看| 亚洲专区中文字幕视频| 国产午夜福利片在线观看| 一二区中文字幕在线观看| 日本久久精品在线观看| 国产成人精品视频一二区| 91超频在线视频中文字幕| 夫妻性生活动态图视频| 日本免费一区二区三女| 在线中文字幕亚洲欧美一区| 一区二区三区亚洲天堂| 国产一级特黄在线观看| 五月激情婷婷丁香六月网| 不卡视频在线一区二区三区| 五月天六月激情联盟网| 亚洲国产性感美女视频| 日韩中文高清在线专区| 日韩精品一级片免费看| 日韩欧美综合中文字幕| 高清不卡一卡二卡区在线| 亚洲欧美一二区日韩高清在线| 又色又爽又黄的三级视频| 亚洲精品福利视频你懂的| 日韩精品毛片视频免费看| 国产国产精品精品在线| 国产91麻豆精品成人区| 国产一区二区三区四区免费| 开心久久综合激情五月天| 亚洲欧洲成人精品香蕉网| 欧美亚洲美女资源国产| 欧美日韩国产精品自在自线| 国产精品久久香蕉国产线| 欧美日韩无卡一区二区| 亚洲成人黄色一级大片| 国产亚洲精品香蕉视频播放| 人妻少妇久久中文字幕久久| 亚洲黑人精品一区二区欧美| 亚洲一区二区亚洲日本| 国内九一激情白浆发布| 夜夜嗨激情五月天精品|