<?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, 30 Jan 2012 13:53:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<div id='fb-root'></div>
					<script type='text/javascript'>
						window.fbAsyncInit = function()
						{
							FB.init({appId: null, status: true, cookie: true, xfbml: true});
						};
						(function()
						{
							var e = document.createElement('script'); e.async = true;
							e.src = document.location.protocol + '//connect.facebook.net/it_IT/all.js';
							document.getElementById('fb-root').appendChild(e);
						}());
					</script>	
						<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. Il template è fatto circa così: 1 2 3 4 5 6 &#60;?php echo __&#40;'Aggiungi prodotto'&#41; ?&#62;&#60;input type=&#34;button&#34; value=&#34;+&#34; onclick=&#34;addprod()&#34; /&#62; &#60;?php echo javascript_tag&#40;&#34; function addprod() { alert('ciao') } &#34;&#41; ?&#62; Se chiamato &#8220;direttamente&#8221;, questo template funziona e visualizzerà correttamente l&#8217;alert. Ciò non [...]]]></description>
			<content:encoded><![CDATA[<p>Oggi dovevo eseguire del codice Javascript nel risultato di una chiamata ajax. Il template è fatto circa così:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Aggiungi prodotto'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;input type=&quot;button&quot; value=&quot;+&quot; onclick=&quot;addprod()&quot; /&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> javascript_tag<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
  function addprod() { 
    alert('ciao')
  }
&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Se chiamato &#8220;direttamente&#8221;, questo template funziona e visualizzerà correttamente l&#8217;alert. Ciò non si verificherà invece se il 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 il template deve essere riscritto nel seguente modo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Aggiungi prodotto'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;input type=&quot;button&quot; value=&quot;+&quot; onclick=&quot;addprod()&quot; /&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> javascript_tag<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
  addprod = function() { 
    alert('alora')
  }
&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>in modo che la funzione venga <em>creata</em>.</p>
<div class='wpfblike' style='height: 40px;'><fb:like href='http://www.symfony.it/articoli/14/javascript-ajax-eval/' layout='default' show_faces='true' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.symfony.it/articoli/14/javascript-ajax-eval/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Calendario nel risultato di una chiamata Ajax</title>
		<link>http://www.symfony.it/articoli/11/calendario-nel-risultato-di-una-chiamata-ajax/</link>
		<comments>http://www.symfony.it/articoli/11/calendario-nel-risultato-di-una-chiamata-ajax/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 14:11:45 +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/11/calendario-nel-risultato-di-una-chiamata-ajax/</guid>
		<description><![CDATA[Oggi dovevo utilizzare l&#8217;helper input_date_tag all&#8217;interno di un template che di fatto era il risultato di una chiamata Ajax. Dato che normalmente non ho mai avuto problemi con quell&#8217;helper mi è risultato strano l&#8217;errore Javascript &#8220;Calendar is not defined&#8221;; dopo un attimo di ricerca ho capito che in tale frangente i Javascript e i css [...]]]></description>
			<content:encoded><![CDATA[<p>Oggi dovevo utilizzare l&#8217;helper <a href="http://www.symfony-project.com/api/symfony/helper/FormHelper.html">input_date_tag</a> all&#8217;interno di un template che di fatto era il risultato di una chiamata Ajax.<br />
Dato che normalmente non ho mai avuto problemi con quell&#8217;helper mi è risultato strano l&#8217;errore Javascript &#8220;Calendar is not defined&#8221;; dopo un attimo di ricerca ho capito che in tale frangente i Javascript e i css non vengono inclusi automaticamente (come invece avviene di solito), per cui ho dovuto aggiungerli nel file di configurazione del modulo view.yml:</p>
<pre>
  stylesheets:    [/sf/calendar/skins/aqua/theme.css]
  javascripts:    [/sf/calendar/calendar.js, /sf/calendar/lang/calendar-it.js, /sf/calendar/calendar-setup.js]
</pre>
<p>et voilà, il calendario funziona di nuovo.</p>
<div class='wpfblike' style='height: 40px;'><fb:like href='http://www.symfony.it/articoli/11/calendario-nel-risultato-di-una-chiamata-ajax/' layout='default' show_faces='true' width='400' action='like' colorscheme='light' send='false' /></div>]]></content:encoded>
			<wfw:commentRss>http://www.symfony.it/articoli/11/calendario-nel-risultato-di-una-chiamata-ajax/feed/</wfw:commentRss>
		<slash:comments>1</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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getCategorie<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Criteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAscendingOrderByColumn</span><span style="color: #009900;">&#40;</span>CategoriePeer<span style="color: #339933;">::</span><span style="color: #004000;">CATEGORIE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">categorie</span> <span style="color: #339933;">=</span> CategoriePeer<span style="color: #339933;">::</span><span style="color: #004000;">doSelect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">categorie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Dopodiché,  nel template, ciclo sulle tuple trovate:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$categorie</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$categoria</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;p class=&quot;categoria&quot;&gt;
&lt;p class=&quot;catname_div&quot;&gt;</pre></td></tr></table></div>

<p>Voglio mettere l&#8217;iconcina con il classico &#8220;+&#8221; nel caso ci sia almeno una sottocategoria, per cui:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Criteria<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span>SottocategoriePeer<span style="color: #339933;">::</span><span style="color: #004000;">RIF_CATEGORIE</span><span style="color: #339933;">,</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Criteria<span style="color: #339933;">::</span><span style="color: #004000;">EQUAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$sottocat_count</span> <span style="color: #339933;">=</span> SottocategoriePeer<span style="color: #339933;">::</span><span style="color: #004000;">doCount</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sottocat_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> image_tag<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'explode.gif'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'img_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<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&#8217;albero che per chiuderlo. Per questo motivo la chiamata ajax deve essere condizionale con un ramo else, cosa non permessa dall&#8217;helper, per cui vado a mano:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;a href=&quot;#&quot; onclick=&quot;if (document.getElementById('sottocat_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>').innerHTML == '') {new Ajax.Updater('sottocat_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>', 'categorie/trovasotto/id/<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>', {asynchronous:true, evalScripts:true, onComplete:function(request, json){document.getElementById('img_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>').src='/images/implode.gif'; Element.hide('indicator');}, onLoading:function(request, json){Element.show('indicator')}}); return false;} else {document.getElementById('sottocat_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>').innerHTML = ''; document.getElementById('img_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>').src='/images/explode.gif'}&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;</pre></td></tr></table></div>

<p>Non faccio altro che dire &#8220;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 &#8220;+&#8221; (leggi: chiudilo), altrimenti fai la chiamata remota e l&#8217;iconcina diventa un &#8220;-&#8221; (leggi: espandilo).</p>
<p>Infine la chiamata remota per poter aggiungere nuove sottocategorie ed i div necessari:</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;">&lt;div id=&quot;addsottocateg_div&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> link_to_remote<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'+'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'categorie/aggiungiSotto?id='</span><span style="color: #339933;">.</span><span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'update'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'acs_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'script'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&lt;div class=&quot;acs&quot; id=&quot;acs_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;sottocategoria_div&quot; id=&quot;sottocat_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$categoria</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdCategorie</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Tutto questo non sarebbe stato necessario se il mitico sfgWidgetsPlugin avesse una opzione &#8220;scripts => true&#8221; per permettere l&#8217;evaluate di script anche nella destinazione.</p>
<div class='wpfblike' style='height: 40px;'><fb:like href='http://www.symfony.it/articoli/9/albero-ajax/' layout='default' show_faces='true' width='400' action='like' colorscheme='light' send='false' /></div>]]></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! -->
