<?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>JeBulle.Net &#187; Traductions</title>
	<atom:link href="http://jebulle.net/category/traductions/feed" rel="self" type="application/rss+xml" />
	<link>http://jebulle.net</link>
	<description>Bullons ensemble</description>
	<lastBuildDate>Tue, 18 Oct 2011 18:46:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<meta name="generator" content="WordPress" />
		<item>
		<title>Trad : Du code propre avec les micro-plugins jQuery</title>
		<link>http://jebulle.net/trad-du-code-propre-avec-les-micro-plugins-jquery</link>
		<comments>http://jebulle.net/trad-du-code-propre-avec-les-micro-plugins-jquery#comments</comments>
		<pubDate>Thu, 03 Dec 2009 18:10:56 +0000</pubDate>
		<dc:creator>SamRay1024</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Traductions]]></category>

		<guid isPermaLink="false">http://jebulle.net/?p=435</guid>
		<description><![CDATA[Rendez vos programmes javascripts plus claires et plus maintenables en utilisant un gabarit de développement uniformisé.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Cet article est la traduction d&#8217;un <a title="Du code plus propre grâce aux micro-plugins jQuery" href="http://www.thegarvin.com/2009/10/28/clearer-code-with-jquery-micro-plugins.html">article publié sur le blog de Jim GARVIN</a>.</p></blockquote>
<p>Beaucoup de programmes <strong>javascripts</strong> nécessitent l&#8217;exécution de code dès lors que le chargement d&#8217;une page est arrivé à terme. <a title="Librairie Javascript jQuery" href="http://jquery.com/">jQuery</a> rend facile le branchement à cet événement et beaucoup de développeurs l&#8217;utilise comme tel :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// code du programme... tout est là</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> term <span style="color: #339933;">=</span> searchParameter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>term<span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search_form input[type=text]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span>term<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>addClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;focus&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// .. 200 lignes plus loin ...</span>
	<span style="color: #666666; font-style: italic;">// ah, voici le code que vous devez modifier</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Ce type de code est très certainement suffisant pour de <strong>petites applications</strong>, mais dès lors que vous avez quelques centaines de lignes de code qui grouillent, il est fort probable que vous commenciez à souffrir pour travailler. Vous pouvez essayer de ponctuer le code de commentaires &#8211; nous savons tous que ce n&#8217;est pas idéal &#8211; ou encore découper le code en plusieurs appels de <em>$document.ready()</em>, les uns après les autres. Pas très propre&#8230;</p>
<p>Une façon d&#8217;obtenir un plus haut-niveau de compréhension de votre code consiste à sortir les sections sans rapport dans leurs propres fonctions pour ensuite les appeler depuis la fonction d&#8217;initialisation :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	initializeSearchForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	initializeRowPointer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	initializeThumbnailSizeSlider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	initializeQueueStatuses<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// et cetera ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> initializeSearchForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> term <span style="color: #339933;">=</span> searchParameter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>term<span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search_form input[type=text]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span>term<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>addClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;focus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// et toutes les autres fonctions ...</span></pre></td></tr></table></div>

<p>Désormais, nous avons une <strong>vue d&#8217;ensemble claire</strong> de ce qui se passe au chargement de la page et nous pouvons <strong>aisément accéder</strong> à telle ou telle fonction pour voir son fonctionnement et lui apporter des modifications. C&#8217;est mieux, et c&#8217;est comme celà que je procède depuis longtemps, mais en utilisant le patron de conception de <a href="http://ryan.mcgeary.org/">Ryan McGeary</a>, nous pouvons rendre ce code encore plus révélateur de ce qu&#8217;il fait et améliorer sa maintenabilité en embraçant le style <a title="Librairie Javascript jQuery" href="http://jquery.com/">jQuery</a>.</p>
<h3>Transformation en micro-plugin</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search input[type=text]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>searchForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#orders&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>selectableRows<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#slider&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>thumbnailSizeSlider<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;td.picture img&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#queue_statuses .status&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>queueStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// et cetera...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// micro-plugin</span>
$<span style="color: #339933;">.</span>fn<span style="color: #339933;">.</span>searchForm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> term <span style="color: #339933;">=</span> searchParameter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>term<span style="color: #009900;">&#41;</span> this<span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span>term<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>addClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;focus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> this<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// et tous les autres micro-plugins...</span></pre></td></tr></table></div>

<p>En réécrivant nos fonctions en tant que <strong>plugins <a title="Librairie Javascript jQuery" href="http://jquery.com/">jQuery</a></strong>, nous bénéficions de nombreux avantages par rapport à la première approche :</p>
<ul>
<li>Nous déplaçons le sélecteur dans la section d&#8217;initialisation où nous pouvons<strong> facilement le retrouvez et le modifier</strong>. Cela nous donne une <strong>meilleure vision</strong> de ce que le code fait, tout cela sans effort.</li>
<li>C&#8217;est un <strong>plugin </strong><strong>jQuery</strong> ! N&#8217;importe quel autre développeur habitué à <strong>jQuery</strong> et ne connaissant pas votre code, pourra immédiatement l&#8217;appréhender et se l&#8217;approprier,</li>
<li>C&#8217;est un <strong>plugin jQuery</strong> ! C&#8217;est facilement <strong>réutilisable</strong>, même si ce n&#8217;est pour l&#8217;instant pas l&#8217;objectif.</li>
</ul>
<p>Si vous n&#8217;aviez jamais écrit de <strong>plugin jQuery </strong>auparavant, ceci devrait vous convaincre de la <strong>simplicité d&#8217;implémentation</strong>. Il n&#8217;est pas nécessaire de comprendre tous les méandres d&#8217;un framework. Vous adjoignez simplement une fonction à <em>jQuery.fn</em> et accédez à l&#8217;objet courant grâce à <em>this. </em>C&#8217;est la <strong>simplicité du système de plugins jQuery</strong> qui rend les <strong>micro-plugins</strong> possibles.</p>
<h3>Quelle est cette pitrerie !? Un plugin, tout simplement !</h3>
<p>Faisons tout de même bien la <strong>distinction </strong>entre un <strong>plugin</strong> et un <strong>micro-plugin</strong>. La différence réside principalement dans l&#8217;<strong>intention de l&#8217;auteur</strong> qui rédige le code. Généralement, les <strong>plugins jQuery </strong>sont écrits prioritairement pour être <strong>réutilisables et partageables</strong>. À l&#8217;inverse, les <strong>micro-plugins</strong> sont créés pour rendre le code local d&#8217;une application <strong>plus clair</strong> et plus facilement <strong>maintenable</strong>, sans intention de partage et/ou de réutilisation.</p>
<p>Ainsi, si vous réutilisez un micro-plugin dans un autre projet, et que ce plugin devient un outil souvent utile, pourquoi pas ne pas le documenter, le partager et le considérer comme plugin à part entière !</p>
<p><em>Note du traducteur : une erreur de traduction, une approximation disgracieuse, une suggestion, &#8230;, utilisez les commentaires pour me soumettre votre proposition et ainsi améliorer cette traduction ! Merci !</em></p>
<p><em> Note bis : les emphases sont de mon fait.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jebulle.net/trad-du-code-propre-avec-les-micro-plugins-jquery/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

