<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dijit &#8211; Sanmao的幸福(?)生活</title>
	<atom:link href="/articles/tag/dijit/feed" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Domino/Notes技术、Ubuntu、TV Game</description>
	<lastBuildDate>Tue, 14 Apr 2009 03:24:46 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.2</generator>
	<item>
		<title>扩展标准的 Dijit 控件</title>
		<link>/articles/extending-a-dijit.html</link>
				<comments>/articles/extending-a-dijit.html#respond</comments>
				<pubDate>Tue, 14 Apr 2009 03:24:46 +0000</pubDate>
		<dc:creator><![CDATA[Sanmao]]></dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[dijit]]></category>
		<category><![CDATA[extend]]></category>

		<guid isPermaLink="false">http://sanmao.yo2.cn/?p=638065</guid>
				<description><![CDATA[标准的 Dojo 包中提供了很多 Dijit 控件，提供了各种丰富的功能。但是用户需求是多种多样的，总有些标准 [&#8230;]]]></description>
								<content:encoded><![CDATA[<p>标准的 Dojo 包中提供了很多 Dijit 控件，提供了各种丰富的功能。但是用户需求是多种多样的，总有些标准控件没有实现的部分。还好 Dojo 提供了多种扩展机制，以适应不同的场景。</p>
<p>接下来以为 dijit.Dialog 扩展一个简单的 setTitle 方法为例，介绍一下三种不同的方式。</p>
<p>1、使用 dojo.extend 直接修改 Dialog 的原型</p>
<blockquote>
<pre>dojo.<span style="color: #660066">require</span><span style="color: #009900">(</span><span style="color: #3366cc">"dijit.Dialog"</span><span style="color: #009900">)</span>;
dojo.<span style="color: #660066">extend</span><span style="color: #009900">(</span>dijit.<span style="color: #660066">Dialog</span><span style="color: #339933">,</span><span style="color: #009900">{</span>
     setTitle<span style="color: #339933">:</span> <span style="font-weight: bold;color: #003366">function</span><span style="color: #009900">(</span><span style="color: #006600;font-style: italic">/*string*/</span>title<span style="color: #009900">)</span><span style="color: #009900">{</span>
         <span style="font-weight: bold;color: #000066">this</span>.<span style="color: #660066">titleNode</span>.<span style="color: #660066">innerHTML</span> <span style="color: #339933">=</span> title || <span style="color: #3366cc">""</span>;
     <span style="color: #009900">}</span>
<span style="color: #009900">}</span><span style="color: #009900">)</span>;</pre>
</blockquote>
<p>2、使用 dojo.mixin 将 setTitle 添加到某个指定的 Dialog 实例</p>
<blockquote>
<pre>dojo.<span style="color: #660066">require</span><span style="color: #009900">(</span><span style="color: #3366cc">"dijit.Dialog"</span><span style="color: #009900">)</span>;
dojo.<span style="color: #660066">addOnLoad</span><span style="color: #009900">(</span><span style="font-weight: bold;color: #003366">function</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
     <span style="font-weight: bold;color: #003366">var</span> dialog <span style="color: #339933">=</span> <span style="font-weight: bold;color: #003366">new</span> dijit.<span style="color: #660066">Dialog</span><span style="color: #009900">(</span><span style="color: #009900">{</span> title<span style="color: #339933">:</span><span style="color: #3366cc">"test"</span> <span style="color: #009900">}</span><span style="color: #009900">)</span>;
     dialog.<span style="color: #660066">startup</span><span style="color: #009900">(</span><span style="color: #009900">)</span>;
     dojo.<span style="color: #660066">mixin</span><span style="color: #009900">(</span>dialog<span style="color: #339933">,</span><span style="color: #009900">{</span>
          setTitle<span style="color: #339933">:</span><span style="font-weight: bold;color: #003366">function</span><span style="color: #009900">(</span>title<span style="color: #009900">)</span><span style="color: #009900">{</span>
               <span style="font-weight: bold;color: #000066">this</span>.<span style="color: #660066">titleNode</span>.<span style="color: #660066">innerHTML</span> <span style="color: #339933">=</span> title || <span style="color: #3366cc">""</span>;
          <span style="color: #009900">}</span>
     <span style="color: #009900">}</span><span style="color: #009900">)</span>;
<span style="color: #009900">}</span><span style="color: #009900">)</span>;</pre>
</blockquote>
<p>3、使用 dojo.declare 定义一个继承自 Dialog 的子类</p>
<blockquote>
<pre>dojo.<span style="color: #660066">provide</span><span style="color: #009900">(</span><span style="color: #3366cc">"my.Dialog"</span><span style="color: #009900">)</span>;
dojo.<span style="color: #660066">require</span><span style="color: #009900">(</span><span style="color: #3366cc">"dijit.Dialog"</span><span style="color: #009900">)</span>;
dojo.<span style="color: #660066">declare</span><span style="color: #009900">(</span><span style="color: #3366cc">"my.Dialog"</span><span style="color: #339933">,</span>dijit.<span style="color: #660066">Dialog</span><span style="color: #339933">,</span><span style="color: #009900">{</span>
     setTitle<span style="color: #339933">:</span><span style="font-weight: bold;color: #003366">function</span><span style="color: #009900">(</span>title<span style="color: #009900">)</span><span style="color: #009900">{</span>
          <span style="font-weight: bold;color: #000066">this</span>.<span style="color: #660066">titleNode</span>.<span style="color: #660066">innerHTML</span> <span style="color: #339933">=</span> title || <span style="color: #3366cc">""</span>;
     <span style="color: #009900">}</span>
<span style="color: #009900">}</span><span style="color: #009900">)</span>;</pre>
</blockquote>
<p>在我的项目中也采用了这种方法，原因如下：一是它保留了原有 Dialog，没有对其进行修改；二是可以通过 inherited 实现往现有的方法中增加代码。例如为 show 方法增加一个判断条件：</p>
<blockquote>
<pre>dojo.<span style="color: #660066">require</span><span style="color: #009900">(</span><span style="color: #3366cc">"dijit.Dialog"</span><span style="color: #009900">)</span>;
dojo.<span style="color: #660066">declare</span><span style="color: #009900">(</span><span style="color: #3366cc">"my.Dialog"</span><span style="color: #339933">,</span>dijit.<span style="color: #660066">Dialog</span><span style="color: #339933">,</span><span style="color: #009900">{</span>
     canBeShown<span style="color: #339933">:</span> <span style="font-weight: bold;color: #003366">true</span><span style="color: #339933">,</span>
     <span style="color: #006600;font-style: italic">// extend the default show() method</span>
     show<span style="color: #339933">:</span><span style="font-weight: bold;color: #003366">function</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
            <span style="font-weight: bold;color: #000066">if</span><span style="color: #009900">(</span><span style="font-weight: bold;color: #000066">this</span>.<span style="color: #660066">canBeShown</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
                    <span style="font-weight: bold;color: #000066">this</span>.<span style="color: #660066">inherited</span><span style="color: #009900">(</span>arguments<span style="color: #009900">)</span>;
            <span style="color: #009900">}</span>
     <span style="color: #009900">}</span>
<span style="color: #009900">}</span><span style="color: #009900">)</span>;</pre>
</blockquote>
<p>参考资料：<a href="http://dojocampus.org/content/2008/02/25/mucking-dijit/" target="_blank">Mucking Dijit</a></p>
]]></content:encoded>
							<wfw:commentRss>/articles/extending-a-dijit.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
							</item>
	</channel>
</rss>
