event-do.js.html revision eb9b6f817df5c660be9f00fc5d27fe33479fdc98
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>event <span class="subtitle">3.00PR1</span></h3>
<p>
> event-do.js (source view)
</p>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form name="yui-classopts-form">
<span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> Show Private</span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> Show Protected</span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> Show Deprecated</span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">"aop"</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">Y</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">BEFORE</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span>
<span class="nx">AFTER</span> <span class="o">=</span> <span class="m">1</span><span class="o">;</span>
<span class="c">/**</span>
<span class="c"> * Allows for the insertion of methods that are executed before or after</span>
<span class="c"> * a specified method</span>
<span class="c"> * @class Do</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span> <span class="o">=</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * Cache of objects touched by the utility</span>
<span class="c"> * @property objs</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">objs</span><span class="o">:</span> <span class="o">{},</span>
<span class="c">/**</span>
<span class="c"> * Execute the supplied method before the specified function</span>
<span class="c"> * @method before</span>
<span class="c"> * @param fn {Function} the function to execute</span>
<span class="c"> * @param obj the object hosting the method to displace</span>
<span class="c"> * @param sFn {string} the name of the method to displace</span>
<span class="c"> * @param c The execution context for fn</span>
<span class="c"> * @return {string} handle for the subscription</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">before</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span> <span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">,</span> <span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">f</span> <span class="o">=</span> <span class="nx">fn</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="o">[</span><span class="nx">fn</span><span class="o">,</span> <span class="nx">c</span><span class="o">].</span><span class="nx">concat</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">4</span><span class="o">,</span> <span class="kc">true</span><span class="o">));</span>
<span class="nx">f</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">bind</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">Y</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">_inject</span><span class="o">(</span><span class="nx">BEFORE</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Execute the supplied method after the specified function</span>
<span class="c"> * @method after</span>
<span class="c"> * @param fn {Function} the function to execute</span>
<span class="c"> * @param obj the object hosting the method to displace</span>
<span class="c"> * @param sFn {string} the name of the method to displace</span>
<span class="c"> * @param c The execution context for fn</span>
<span class="c"> * @return {string} handle for the subscription</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">after</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span> <span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">,</span> <span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">f</span> <span class="o">=</span> <span class="nx">fn</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="o">[</span><span class="nx">fn</span><span class="o">,</span> <span class="nx">c</span><span class="o">].</span><span class="nx">concat</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">4</span><span class="o">,</span> <span class="kc">true</span><span class="o">));</span>
<span class="nx">f</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">bind</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">Y</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">_inject</span><span class="o">(</span><span class="nx">AFTER</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Execute the supplied method after the specified function</span>
<span class="c"> * @method _inject</span>
<span class="c"> * @param when {string} before or after</span>
<span class="c"> * @param fn {Function} the function to execute</span>
<span class="c"> * @param obj the object hosting the method to displace</span>
<span class="c"> * @param sFn {string} the name of the method to displace</span>
<span class="c"> * @param c The execution context for fn</span>
<span class="c"> * @return {string} handle for the subscription</span>
<span class="c"> * @private</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">_inject</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">when</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// object id</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">id</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">obj</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(!</span> <span class="k">this</span><span class="o">.</span><span class="nx">objs</span><span class="o">[</span><span class="nx">id</span><span class="o">])</span> <span class="o">{</span>
<span class="c">// create a map entry for the obj if it doesn't exist</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">objs</span><span class="o">[</span><span class="nx">id</span><span class="o">]</span> <span class="o">=</span> <span class="o">{};</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">o</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">objs</span><span class="o">[</span><span class="nx">id</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(!</span> <span class="nx">o</span><span class="o">[</span><span class="nx">sFn</span><span class="o">])</span> <span class="o">{</span>
<span class="c">// create a map entry for the method if it doesn't exist</span>
<span class="c"></span> <span class="nx">o</span><span class="o">[</span><span class="nx">sFn</span><span class="o">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">Method</span><span class="o">(</span><span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">);</span>
<span class="c">// re-route the method to our wrapper</span>
<span class="c"></span> <span class="nx">obj</span><span class="o">[</span><span class="nx">sFn</span><span class="o">]</span> <span class="o">=</span>
<span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">o</span><span class="o">[</span><span class="nx">sFn</span><span class="o">].</span><span class="nx">exec</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">o</span><span class="o">[</span><span class="nx">sFn</span><span class="o">],</span> <span class="nx">arguments</span><span class="o">);</span>
<span class="o">};</span>
<span class="o">}</span>
<span class="c">// subscriber id</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">sid</span> <span class="o">=</span> <span class="nx">id</span> <span class="o">+</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">fn</span><span class="o">)</span> <span class="o">+</span> <span class="nx">sFn</span><span class="o">;</span>
<span class="c">// register the callback</span>
<span class="c"></span> <span class="nx">o</span><span class="o">[</span><span class="nx">sFn</span><span class="o">].</span><span class="nx">register</span><span class="o">(</span><span class="nx">sid</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">when</span><span class="o">);</span>
<span class="k">return</span> <span class="nx">sid</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Detach a before or after subscription</span>
<span class="c"> * @method detach</span>
<span class="c"> * @param sid {string} the subscription handle</span>
<span class="c"> */</span>
<span class="nx">detach</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">sid</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">delete</span> <span class="k">this</span><span class="o">.</span><span class="nx">before</span><span class="o">[</span><span class="nx">sid</span><span class="o">];</span>
<span class="nx">delete</span> <span class="k">this</span><span class="o">.</span><span class="nx">after</span><span class="o">[</span><span class="nx">sid</span><span class="o">];</span>
<span class="o">},</span>
<span class="nx">_unload</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">e</span><span class="o">,</span> <span class="nx">me</span><span class="o">)</span> <span class="o">{</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">//////////////////////////////////////////////////////////////////////////</span>
<span class="c"></span>
<span class="c">/**</span>
<span class="c"> * Wrapper for a displaced method with aop enabled</span>
<span class="c"> * @constructor</span>
<span class="c"> * @param obj The object to operate on</span>
<span class="c"> * @param sFn The name of the method to displace</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">Method</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">obj</span><span class="o">,</span> <span class="nx">sFn</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">obj</span> <span class="o">=</span> <span class="nx">obj</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">methodName</span> <span class="o">=</span> <span class="nx">sFn</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">method</span> <span class="o">=</span> <span class="nx">obj</span><span class="o">[</span><span class="nx">sFn</span><span class="o">];</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">before</span> <span class="o">=</span> <span class="o">{};</span>
<span class="k">this</span><span class="o">.</span><span class="nx">after</span> <span class="o">=</span> <span class="o">{};</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Register a aop subscriber</span>
<span class="c"> * @method register</span>
<span class="c"> * @param sid {string} the subscriber id</span>
<span class="c"> * @param fn {Function} the function to execute</span>
<span class="c"> * @param when {string} when to execute the function</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">Method</span><span class="o">.</span><span class="nx">prototype</span><span class="o">.</span><span class="nx">register</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">sid</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">when</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">when</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">after</span><span class="o">[</span><span class="nx">sid</span><span class="o">]</span> <span class="o">=</span> <span class="nx">fn</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">before</span><span class="o">[</span><span class="nx">sid</span><span class="o">]</span> <span class="o">=</span> <span class="nx">fn</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Execute the wrapped method</span>
<span class="c"> * @method exec</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">Method</span><span class="o">.</span><span class="nx">prototype</span><span class="o">.</span><span class="nx">exec</span> <span class="o">=</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">),</span>
<span class="nx">i</span><span class="o">,</span> <span class="nx">ret</span><span class="o">,</span> <span class="nx">newRet</span><span class="o">,</span>
<span class="nx">bf</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">before</span><span class="o">,</span>
<span class="nx">af</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">after</span><span class="o">;</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="k">in</span> <span class="nx">bf</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">bf</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">i</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">ret</span> <span class="o">=</span> <span class="nx">bf</span><span class="o">[</span><span class="nx">i</span><span class="o">].</span><span class="nx">apply</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">obj</span><span class="o">,</span> <span class="nx">args</span><span class="o">);</span>
<span class="c">// Stop processing if an Error is returned</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">ret</span> <span class="o">&&</span> <span class="nx">ret</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">==</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nb">Error</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="k">return</span> <span class="nx">ret</span><span class="o">.</span><span class="nx">retVal</span><span class="o">;</span>
<span class="c">// Check for altered arguments</span>
<span class="c"></span> <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">ret</span> <span class="o">&&</span> <span class="nx">ret</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">==</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">AlterArgs</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">ret</span><span class="o">.</span><span class="nx">newArgs</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// execute method</span>
<span class="c"></span> <span class="nx">ret</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">method</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">obj</span><span class="o">,</span> <span class="nx">args</span><span class="o">);</span>
<span class="c">// execute after methods.</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="k">in</span> <span class="nx">af</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">af</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">i</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">newRet</span> <span class="o">=</span> <span class="nx">af</span><span class="o">[</span><span class="nx">i</span><span class="o">].</span><span class="nx">apply</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">obj</span><span class="o">,</span> <span class="nx">args</span><span class="o">);</span>
<span class="c">// Stop processing if an Error is returned</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">newRet</span> <span class="o">&&</span> <span class="nx">newRet</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">==</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nb">Error</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="k">return</span> <span class="nx">newRet</span><span class="o">.</span><span class="nx">retVal</span><span class="o">;</span>
<span class="c">// Check for a new return value</span>
<span class="c"></span> <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">newRet</span> <span class="o">&&</span> <span class="nx">newRet</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">==</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">AlterReturn</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">newRet</span><span class="o">.</span><span class="nx">newRetVal</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">ret</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">//////////////////////////////////////////////////////////////////////////</span>
<span class="c"></span>
<span class="c">/**</span>
<span class="c"> * Return an Error object when you want to terminate the execution</span>
<span class="c"> * of all subsequent method calls</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nb">Error</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">msg</span><span class="o">,</span> <span class="nx">retVal</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">msg</span> <span class="o">=</span> <span class="nx">msg</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">retVal</span> <span class="o">=</span> <span class="nx">retVal</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Return an AlterArgs object when you want to change the arguments that</span>
<span class="c"> * were passed into the function. An example would be a service that scrubs</span>
<span class="c"> * out illegal characters prior to executing the core business logic.</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">AlterArgs</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">msg</span><span class="o">,</span> <span class="nx">newArgs</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">msg</span> <span class="o">=</span> <span class="nx">msg</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">newArgs</span> <span class="o">=</span> <span class="nx">newArgs</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Return an AlterReturn object when you want to change the result returned</span>
<span class="c"> * from the core method to the caller</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">AlterReturn</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">msg</span><span class="o">,</span> <span class="nx">newRetVal</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">msg</span> <span class="o">=</span> <span class="nx">msg</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">newRetVal</span> <span class="o">=</span> <span class="nx">newRetVal</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">//////////////////////////////////////////////////////////////////////////</span>
<span class="c"></span>
<span class="c">// Y["Event"] && Y.Event.addListener(window, "unload", Y.Do._unload, Y.Do);</span>
<span class="c"></span>
<span class="o">},</span> <span class="s2">"3.0.0"</span><span class="o">);</span>
</pre></div>
</div>
</div>
</div>
<div class="yui-b">
<div class="nav">
<div class="module">
<h4>Modules</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>