<?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>Symfony-IT &#187; ajax</title>
	<atom:link href="http://www.symfony.it/categoria/programmazione/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.symfony.it</link>
	<description>Just another Symfony Framework weblog</description>
	<lastBuildDate>Mon, 26 Jul 2010 09:23:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Javascript &amp; Ajax? eval()!</title>
		<link>http://www.symfony.it/articoli/14/javascript-ajax-eval/</link>
		<comments>http://www.symfony.it/articoli/14/javascript-ajax-eval/#comments</comments>
		<pubDate>Fri, 10 Aug 2007 09:13:52 +0000</pubDate>
		<dc:creator>giorg</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.symfony.it/articoli/14/javascript-ajax-eval/</guid>
		<description><![CDATA[Oggi dovevo eseguire del codice Javascript nel risultato di una chiamata ajax. La template è fatta circa così: PLAIN TEXT CODE: &#60;?php echo __&#40;'Aggiungi prodotto'&#41; ?&#62;&#60;input type="button" value="+" onclick="addprod()" /&#62; &#60;?php echo javascript_tag&#40;" &#160; &#160; function addprod() { &#160; &#160; &#160; &#160; alert('ciao') &#160; &#160; } "&#41; ?&#62; Se chiamata "direttamente", questa template funziona è [...]]]></description>
			<content:encoded><![CDATA[<p>Oggi dovevo eseguire del codice Javascript nel risultato di una chiamata ajax. La template è fatta circa così:</p>
<div class="igBar"><span id="lcode-3"><a href="#" onclick="javascript:showPlainTxt('code-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-3">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php echo __<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'Aggiungi prodotto'</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?&gt;&lt;input type=<span style="color:#CC0000;">"button"</span> value=<span style="color:#CC0000;">"+"</span> onclick=<span style="color:#CC0000;">"addprod()"</span> /&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php echo javascript_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">&nbsp; &nbsp; function addprod() { </span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">&nbsp; &nbsp; &nbsp; &nbsp; alert('ciao')</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">&nbsp; &nbsp; }</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">"</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Se chiamata "direttamente", questa template funziona è visualizzerà correttamente l'alert. Ciò non si verificherà invece se la template in questione è il risultato di una chiamata Ajax. Come <a href="http://www.sergiopereira.com/articles/prototype.js.html#UsingAjaxUpdater">spiega il buon Sergio Pereira</a>, ciò dipende dal fatto che il codice Javascript nel risultato di una chiamata ajax viene <strong>valutato</strong> (tramite la funzione eval()) per cui la template deve essere riscritta nel seguente modo:</p>
<div class="igBar"><span id="lcode-4"><a href="#" onclick="javascript:showPlainTxt('code-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-4">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php echo __<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'Aggiungi prodotto'</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?&gt;&lt;input type=<span style="color:#CC0000;">"button"</span> value=<span style="color:#CC0000;">"+"</span> onclick=<span style="color:#CC0000;">"addprod()"</span> /&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php echo javascript_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">&nbsp; &nbsp; addprod = function() { </span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">&nbsp; &nbsp; &nbsp; &nbsp; alert('alora')</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">&nbsp; &nbsp; }</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0000;">"</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>in modo che la funzione venga <em>creata</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.symfony.it/articoli/14/javascript-ajax-eval/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Albero ajax</title>
		<link>http://www.symfony.it/articoli/9/albero-ajax/</link>
		<comments>http://www.symfony.it/articoli/9/albero-ajax/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 15:56:07 +0000</pubDate>
		<dc:creator>giorg</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://www.symfony.it/articoli/9/albero-ajax/</guid>
		<description><![CDATA[Ho avuto il problema di creare un albero prendendo categorie e subcategorie dal database; il tutto con una certa user experience, per cui possibilmente ajax. Avevo provato il plugin sfgWidgetsPlugin ma non permette più di un innesto (manca qualcosa tipo evalScript: true nella chiamata ajax), per cui ho dovuto fare a mano. A seguito la [...]]]></description>
			<content:encoded><![CDATA[<p>Ho avuto il problema di creare un albero prendendo categorie e subcategorie dal database; il tutto con una certa user experience, per cui possibilmente ajax. Avevo provato il plugin <a href="http://trac.symfony-project.com/trac/wiki/sfgWidgetsPlugin">sfgWidgetsPlugin</a> ma non permette più di un innesto (manca qualcosa tipo evalScript: true nella chiamata ajax), per cui ho dovuto fare a mano.</p>
<p>A seguito la mia soluzione.</p>
<p><span id="more-9"></span></p>
<p>Prima di tutto nella mia action tiro fuori la prima serie di categorie:</p>
<div class="igBar"><span id="lcode-10"><a href="#" onclick="javascript:showPlainTxt('code-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-10">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">private function getCategorie<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$c = new Criteria<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$c-&gt;<span style="">addAscendingOrderByColumn</span><span style="color:#006600; font-weight:bold;">&#40;</span>CategoriePeer::<span style="">CATEGORIE</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$this-&gt;<span style="">categorie</span> = CategoriePeer::<span style="">doSelect</span><span style="color:#006600; font-weight:bold;">&#40;</span>$c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$this-&gt;<span style="">forward404Unless</span><span style="color:#006600; font-weight:bold;">&#40;</span>$this-&gt;<span style="">categorie</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Dopodichè,  nella template, ciclo sulle tuple trovate:</p>
<div class="igBar"><span id="lcode-11"><a href="#" onclick="javascript:showPlainTxt('code-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-11">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php foreach <span style="color:#006600; font-weight:bold;">&#40;</span>$categorie as $categoria<span style="color:#006600; font-weight:bold;">&#41;</span>: ?&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;p class=<span style="color:#CC0000;">"categoria"</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;p class=<span style="color:#CC0000;">"catname_div"</span>&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Voglio mettere l'iconcina con il classico "+" nel caso ci sia almeno una sottocategoria, per cui:</p>
<div class="igBar"><span id="lcode-12"><a href="#" onclick="javascript:showPlainTxt('code-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-12">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php $c = new Criteria<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; $c-&gt;<span style="">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>SottocategoriePeer::<span style="">RIF_CATEGORIE</span>, $categoria-&gt;<span style="">getIdCategorie</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, Criteria::<span style="">EQUAL</span><span style="color:#006600; font-weight:bold;">&#41;</span>; $sottocat_count = SottocategoriePeer::<span style="">doCount</span><span style="color:#006600; font-weight:bold;">&#40;</span>$c<span style="color:#006600; font-weight:bold;">&#41;</span>; ?&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php if <span style="color:#006600; font-weight:bold;">&#40;</span>$sottocat_count&gt; <span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> echo image_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'explode.gif'</span>, array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'id'</span> =&gt; <span style="color:#CC0000;">'img_'</span>.$categoria-&gt;<span style="">getIdCategorie</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; ?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ora ci occorrerebbe il famoso helper link_to_remote per poter accedere alle sub-sub-categorie; però mi piacerebbe poter clikkare sul nome di una categoria sia per espandere l'albero che per chiuderlo. Per questo motivo la chiamata ajax deve essere condizionale con un ramo else, cosa non permessa dall'helper, per cui vado a mano:</p>
<div class="igBar"><span id="lcode-13"><a href="#" onclick="javascript:showPlainTxt('code-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-13">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;a href=<span style="color:#CC0000;">"#"</span> onclick=<span style="color:#CC0000;">"if (document.getElementById('sottocat_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;').innerHTML == '') {new Ajax.Updater('sottocat_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;', 'categorie/trovasotto/id/&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;', {asynchronous:true, evalScripts:true, onComplete:function(request, json){document.getElementById('img_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;').src='/images/implode.gif'; Element.hide('indicator');}, onLoading:function(request, json){Element.show('indicator')}}); return false;} else {document.getElementById('sottocat_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;').innerHTML = ''; document.getElementById('img_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;').src='/images/explode.gif'}"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php echo $categoria-&gt;<span style="">getCategorie</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?&gt;&lt;/a&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Non faccio altro che dire "se il div che riempio tramite la chiamata remota ha del contenuto (leggi: è espanso) allora setta il suo contenuto a vuoto e metti come iconcina di nuovo il "+" (leggi: chiudilo), altrimenti fai la chiamata remota e l'iconcina diventa un "-" (leggi: espandilo).</p>
<p>Infine la chiamata remota per poter aggiungere nuove sottocategorie ed i div necessari:</p>
<div class="igBar"><span id="lcode-14"><a href="#" onclick="javascript:showPlainTxt('code-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-14">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;div id=<span style="color:#CC0000;">"addsottocateg_div"</span>&gt;&lt;?php echo link_to_remote<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'+'</span>, array<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0000;">'url'</span> =&gt; <span style="color:#CC0000;">'categorie/aggiungiSotto?id='</span>.$categoria-&gt;<span style="">getIdCategorie</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0000;">'update'</span> =&gt; <span style="color:#CC0000;">'acs_'</span>.$categoria-&gt;<span style="">getIdCategorie</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0000;">'script'</span> =&gt; true,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;/div&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;div class=<span style="color:#CC0000;">"acs"</span> id=<span style="color:#CC0000;">"acs_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;"</span>&gt;&lt;/div&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;div class=<span style="color:#CC0000;">"sottocategoria_div"</span> id=<span style="color:#CC0000;">"sottocat_&lt;?php echo $categoria-&gt;getIdCategorie() ?&gt;"</span>&gt;&lt;/div&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/div&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php endforeach; ?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Tutto questo non sarebbe stato necessario se il mitico sfgWidgetsPlugin avesse una opzione "scripts => true" per permettere l'evaluate di script anche nella destinazione.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.symfony.it/articoli/9/albero-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->