<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' version='2.0'><channel><atom:id>tag:blogger.com,1999:blog-1141629632747507111</atom:id><lastBuildDate>Mon, 11 Jan 2010 08:47:33 +0000</lastBuildDate><title>Nic's blog</title><description></description><link>http://www.nicolarizzo.com/blog/</link><managingEditor>noreply@blogger.com (Nicola Rizzo)</managingEditor><generator>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-1141629632747507111.post-5541323917250940859</guid><pubDate>Wed, 09 Jul 2008 20:17:00 +0000</pubDate><atom:updated>2008-07-12T20:02:39.437+02:00</atom:updated><category domain='http://www.blogger.com/atom/ns#'>javascript</category><category domain='http://www.blogger.com/atom/ns#'>cross framework</category><category domain='http://www.blogger.com/atom/ns#'>jquery</category><category domain='http://www.blogger.com/atom/ns#'>dojo</category><category domain='http://www.blogger.com/atom/ns#'>portability</category><title>Cross framework javascript - coding for portability</title><description>Imagine that you have written a useful javascript widget using your preferred library (dojo), but at work you must use another framework (jQuery), and a customer needs that you code with Prototype.&lt;br /&gt;Now, you can modify your component by hand, replacing the ajax calls (eg from dojo.xhrGet to $.get), the event handling (from dojo.connect to obj.observe...), the effects... What a boring job!&lt;br /&gt;&lt;br /&gt;Or you can write a wrapper who dynamically identifies the js library you are using and maps the ajax calls (and the event handlers, etc...) to the correct form for you.&lt;br /&gt;We can write code like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;     var engine = com.nicolarizzo.utils.runtime.currentEngine;&lt;br /&gt;     var showMessage = function(){&lt;br /&gt;         window.alert("that's all!");&lt;br /&gt;     };&lt;br /&gt;     var startup = function(){&lt;br /&gt;         engine.byId("currentEngine").innerHTML = com.nicolarizzo.utils.runtime.engineName;&lt;br /&gt;         engine.connect(engine.byId("clickMe"), "onclick", this, showMessage);&lt;br /&gt;     };&lt;br /&gt;     engine.addOnLoad(startup);&lt;br /&gt;&lt;/pre&gt;without the knowledge of the underlying frameworks!&lt;br /&gt;&lt;br /&gt;Voilà, &lt;a href="http://www.nicolarizzo.com/gamesroom/crossFramework.html"&gt;here&lt;/a&gt; a proof of concept, incomplete and inaccurate as usual, using jQuery :)&lt;br /&gt;&lt;br /&gt;The mapping is very simple:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// jQuery mappings&lt;br /&gt;com.nicolarizzo.utils.runtime.currentEngine = {&lt;br /&gt;  addOnLoad: function(fnToExecute){&lt;br /&gt;      return jQuery(document).ready.call(jQuery, fnToExecute);&lt;br /&gt;  },&lt;br /&gt;  connect: function(trg, evt, context, method){&lt;br /&gt;      if(evt.indexOf("on") == 0){&lt;br /&gt;          evt = evt.substring(2);&lt;br /&gt;      }&lt;br /&gt;      context = context || trg;&lt;br /&gt;      jQuery(trg).bind(evt, function(){method.call(context)});&lt;br /&gt;  },&lt;br /&gt;  byId: function(id){&lt;br /&gt;      return jQuery("#" + id)[0];&lt;br /&gt;  }&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Isn't it nice? ;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1141629632747507111-5541323917250940859?l=www.nicolarizzo.com%2Fblog' alt='' /&gt;&lt;/div&gt;</description><link>http://www.nicolarizzo.com/blog/2008/07/cross-framework-javascript-coding-for.html</link><author>noreply@blogger.com (Nicola Rizzo)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-1141629632747507111.post-4155744328986199531</guid><pubDate>Sun, 15 Jun 2008 08:58:00 +0000</pubDate><atom:updated>2008-06-15T22:54:19.584+02:00</atom:updated><category domain='http://www.blogger.com/atom/ns#'>javascript</category><category domain='http://www.blogger.com/atom/ns#'>very lazy loading</category><category domain='http://www.blogger.com/atom/ns#'>transparent lazy loading</category><category domain='http://www.blogger.com/atom/ns#'>ajax</category><category domain='http://www.blogger.com/atom/ns#'>dojo</category><title>(Very) lazy loading in javascript</title><description>Using the "lazy loading" in javascript is a common technique to minimize the startup time of a web application.&lt;br /&gt;Usually a single object is retrieved from the server with its own members and methods: but why not use instead of the methods a stub which loads the method source when is called the first time?&lt;br /&gt;For example, a singleton object like this, having two methods:&lt;br /&gt;&lt;pre&gt;var application = {&lt;br /&gt;sum: function(){&lt;br /&gt;    return arguments[0] + arguments[1];&lt;br /&gt;},&lt;br /&gt;product: function(){&lt;br /&gt;    return arguments[0] * arguments[1];&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;could be written as (you can use your preferred ajax framework/library; remember: use a synchronous call):&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;var application = {};&lt;br /&gt;var names = {&lt;br /&gt;"sum" : "sum.js",&lt;br /&gt;"product" : "product.js"&lt;br /&gt;};&lt;br /&gt;for(var i in names){&lt;br /&gt;application[i] = function(x){&lt;br /&gt;  return function(){&lt;br /&gt;      dojo.xhrGet({&lt;br /&gt;          url: names[x],&lt;br /&gt;          sync:true,&lt;br /&gt;          handleAs:"text",&lt;br /&gt;          load: function(data){&lt;br /&gt;              application[x] = new Function(data.replace(/\n/g, ""));&lt;br /&gt;          },&lt;br /&gt;          error: function(err){&lt;br /&gt;              window.alert("[error] " + err);&lt;br /&gt;          }&lt;br /&gt;      });&lt;br /&gt;      return application[x].apply(this, arguments);&lt;br /&gt;  }&lt;br /&gt;}(i);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Oh yes, this is a stupid example, the stub method is greater than the real method, but it's only a proof of concept ;)&lt;br /&gt;You can find a live example &lt;a href="http://www.nicolarizzo.com/gamesroom/veryLazyLoading.html"&gt;here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1141629632747507111-4155744328986199531?l=www.nicolarizzo.com%2Fblog' alt='' /&gt;&lt;/div&gt;</description><link>http://www.nicolarizzo.com/blog/2008/06/placeholder.html</link><author>noreply@blogger.com (Nicola Rizzo)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></item></channel></rss>
