<?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>Appcelerant &#187; Documentation</title>
	<atom:link href="http://www.appcelerant.com/category/documentation/feed" rel="self" type="application/rss+xml" />
	<link>http://www.appcelerant.com</link>
	<description>Appcelerator blog focused on helping you build your next killer mobile, desktop and web based applications</description>
	<lastBuildDate>Thu, 24 Jun 2010 17:43:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Appcelerator Interceptors for I18N, L10N, G11N</title>
		<link>http://www.appcelerant.com/using-appcelerator-interceptors-for-i18n-l10n-g11n.html</link>
		<comments>http://www.appcelerant.com/using-appcelerator-interceptors-for-i18n-l10n-g11n.html#comments</comments>
		<pubDate>Fri, 17 Oct 2008 15:10:10 +0000</pubDate>
		<dc:creator>Andrew Zuercher</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/?p=93</guid>
		<description><![CDATA[What&#8217;s the problem?
Just implementing resource bundles to localize your application doesn&#8217;t quite cut it.  You need to do things like work with localized dates in the browser (including formatting), numbers, currencies, and who knows what else to munge your data.  It sure would be nice if there were a way to transparently make [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Using Appcelerator Interceptors for I18N, L10N, G11N", url: "http://www.appcelerant.com/using-appcelerator-interceptors-for-i18n-l10n-g11n.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><h3>What&#8217;s the problem?</h3>
<p>Just implementing resource bundles to localize your application doesn&#8217;t quite cut it.  You need to do things like work with localized dates in the browser (including formatting), numbers, currencies, and who knows what else to munge your data.  It sure would be nice if there were a way to transparently make this happen in a message oriented way that does not require server side awareness for services, can you help me with this problem?  sure.</p>
<h3>Built in &#8216;ization&#8217; support in Appcelerator</h3>
<p>Globalization (G11N) and Localization (L10N) both get lumped into Internationalization (I18N) &#8211; how to deal with languages, time, data formats and everything else that goes on making it so that people in different places in the world can use your glorious application.  Appcelerator <a href="http://doc.appcelerator.org/reference/localization/index.html">localization</a> helps you out with this on some really simple levels with the langid attribute:</p>

<div class="wp_syntax"><div class="code"><pre class="xml">  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;span</span> <span style="color: #000066;">langid</span>=<span style="color: #ff0000;">&quot;account.number.title&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/span<span style="font-weight: bold; color: black;">&gt;</span></span></span>:<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/input<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>This should arm you successfully with an ability to deal with things like simple messages and labeling in your application.  Now if you want to blend in your data payload to render localized text, you can make use of appcelerator javascript functions Appcelerator.Localization.get and the more powerful but less documented Appcelerator.Localization.getWithFormat.</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  var en_lang_bundle = {
	&quot;invalidlogin.message&quot;: 
&quot;Dear sir/madame, the username you provided '#{username}' does not exist:&quot;
  }
  Appcelerator.Localization.addLanguageBundle(&quot;en&quot;,English&quot;,$H(en_lang_bundle));
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;span</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;l:invalidlogin.response then value[text]&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/span<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;app:script</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;r:invalidlogin.response then execute&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  var text = Appcelerator.Localization.getWithFormat('invalidlogin.message',
null,null,{username:'azuercher'});
  $MQ('l:invalidlogin.response',{text:text});
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/app:script<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>This is a nice way to use message indirection for munging messages, but really is quite messy and anything but transparent.  There must be a better way!</p>
<h3>Interceptors</h3>
<p>A lesser known feature of Appcelerator is the concept of interceptors &#8211; client side behavior that you can have play in-between the delivery of queued messages to their recipients.  In the case for client-2-server orchestration all server requests and their corresponding response messages are enqueued and subject to perversion by interceptors.  You just have to know how to implement them.  Lets say that whenever I send a CRUD request to the server called &#8216;r:createorder.request&#8217; I want to add a new attribute called last_modified_utc which is a numerical representation of a utc date as a long.  I can add that to the payload to the back end service completely transparently with the Appcelerator.Util.ServiceBroker.addInterceptor method &#8211; the method takes a callback to the following:</p>
<ul>
<li>interceptQueue &#8211; where requests from client to server are intercepted</li>
<li>interceptSendToListener &#8211; where responses from server to client are intercepted</li>
</ul>
<p>In the example below I</p>
<ul>
<li>add &#8216;last_modified_utc&#8217; as a current utc long to r:createorder.request which is consumed by my back end service</li>
<li>transform &#8216;last_modified_utc&#8217; in the r:createorder.response from utc to a localized string in the browser</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #66cc66;">&lt;</span>app:script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">var</span> interceptor = <span style="color: #66cc66;">&#123;</span>
	    interceptQueue: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>msg, callback, messageType, scope<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>messageType==<span style="color: #3366CC;">'remote:createorder.request'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				msg.<span style="color: #006600;">data</span>.<span style="color: #006600;">last_modified_utc</span> = <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
	        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
	    <span style="color: #66cc66;">&#125;</span>,
	    interceptDispatch: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>requestid, type, data, datatype<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
	    <span style="color: #66cc66;">&#125;</span>,
	    interceptSendToListener: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>listener, messageType, data, datatype<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>messageType==<span style="color: #3366CC;">'createorder.response'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				date = <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span>data.<span style="color: #006600;">last_modified_utc</span><span style="color: #66cc66;">&#41;</span>;
				data.<span style="color: #006600;">last_modified</span> = date.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
	        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
	    <span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>;
	Appcelerator.<span style="color: #006600;">Util</span>.<span style="color: #006600;">ServiceBroker</span>.<span style="color: #006600;">addInterceptor</span><span style="color: #66cc66;">&#40;</span>interceptor<span style="color: #66cc66;">&#41;</span>;
	$MQ<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'r:createorder.request'</span>,<span style="color: #66cc66;">&#123;</span>hi:<span style="color: #3366CC;">'there'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&lt;/</span>app:script<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Obviously, you can take this a step further and then add interceptors that munge your data conditionally based on the current locale defined in Appcelerator.Localization.  The benefit here is that this data transformation happens at a layer outside of my normal pub/sub for messages in Appcelerator &#8211; the normal pattern is to either replace or add attributes in the payload.  </p>
<h3>wrap up</h3>
<p>Although not a shrink wrapped solution for I18N in all its glory, its not hard to implement interceptors to do so.  Beyond I18N, a lot of other aspects for your messages can be mutated in the browser non-intrusively.  Although the key demonstration here is data transformation, you would potentially add other horizontal services into your application.</p>
<p>
this article cross-posted at <a href="http://blog.zuerchtech.com/2008/10/13/using-appcelerator-interceptors-for-i18n-l10n-g11n/">http://blog.zuerchtech.com/2008/10/13/using-appcelerator-interceptors-for-i18n-l10n-g11n</a></p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Using+Appcelerator+Interceptors+for+I18N%2C+L10N%2C+G11N&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fusing-appcelerator-interceptors-for-i18n-l10n-g11n.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/using-appcelerator-interceptors-for-i18n-l10n-g11n.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deep-linking into RIAs</title>
		<link>http://www.appcelerant.com/deep-linking-into-rias.html</link>
		<comments>http://www.appcelerant.com/deep-linking-into-rias.html#comments</comments>
		<pubDate>Fri, 05 Sep 2008 14:32:58 +0000</pubDate>
		<dc:creator>Mark Luffel</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/?p=81</guid>
		<description><![CDATA[Deep-linking is important. It&#8217;s what makes the web so &#8230; webby. Usability guru Jakob Nielsen tells us that Deep Linking is Good Linking, and hypertext pioneer Doug Engelbart encourages us to make &#8220;Every Object Addressable.&#8221;
With the old-web, deep-linking was easy because everything was a page and every page had a URL. In the new RIA [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Deep-linking into RIAs", url: "http://www.appcelerant.com/deep-linking-into-rias.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><p>Deep-linking is important. It&#8217;s what makes the web so &#8230; webby. Usability guru Jakob Nielsen tells us that <a href="http://www.useit.com/alertbox/20020303.html">Deep Linking is Good Linking</a>, and hypertext pioneer Doug Engelbart encourages us to make &#8220;<a href="http://www.bootstrap.org/augdocs/augment-132082.htm#11K">Every Object Addressable</a>.&#8221;</p>
<p>With the old-web, deep-linking was easy because everything was a page and every page had a URL. In the new RIA web however, a complex web app can fit into a single page, and without some careful programming, we lose the ability to bookmark or share objects and places inside the app.</p>
<p>To get deep-linking in an RIA, we need to store the link info in the <a href="http://www.unix.com.ua/orelly/web/jscript/refp_419.html">hash part</a> of the URL. For example, the Google Docreader team has documented their scheme for <a href="http://code.google.com/docreader/#p=google-documentation-reader&amp;s=google-documentation-reader&amp;t=UrlFormat">deep-linking into ajax-ified documentation</a>. Deep-linking to Flash content with <a href="http://www.asual.com/swfaddress/">SWFAddress</a> also takes advantage of the hash component of a URL.</p>
<p>In Appcelerator, a convenient way to provide deep-linkable addresses (as well as sane forward-backward button support) is to use <a href="http://appceleratordemo.appspot.com/routes">Appcelerator Routes</a> to map the hash part of a URL onto an Appcelerator message. Routes have a similar syntax to <a href="http://manuals.rubyonrails.com/read/chapter/65">Ruby on Rails routes</a>, but are entirely client-side.</p>
<p>An example routes definition:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">routes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #3366CC;">'#home'</span> :                  <span style="color: #3366CC;">'r:get.home.request'</span>,
    <span style="color: #3366CC;">'#cat/:id'</span> :               <span style="color: #3366CC;">'r:get.animal.request[type=cat,id=#{id}]'</span>,
    <span style="color: #3366CC;">'#dog/:id'</span> :               <span style="color: #3366CC;">'r:get.animal.request[type=dog,id=#{id}]'</span>,
    <span style="color: #3366CC;">'#yaps/:fromid/:toid'</span> :    <span style="color: #3366CC;">'r:get.messages.request[dogid=#{fromid},catid=#{toid}]'</span>,
    <span style="color: #3366CC;">'#hisses/:fromid/:toid'</span> :  <span style="color: #3366CC;">'r:get.messages.request[catid=#{fromid},dogid=#{toid}]'</span>,
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, when someone navigates to <tt>http://yourapp.com/pets#dog/fido</tt>, the message <tt>r:get.animal.request[type=dog,id=fido]</tt> will be fired on page load. Inside your UI code, you can react to the response of this message to switch the active section of your app:</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;r:get.animal.response[type=dog] then show and bind[dog_info] else hide&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  My Cat Friends:
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span> app:iterator <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;r:get.animal.request[type=dog] then execute&quot;</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;dog_info.cat_friends&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#cat/#{cat_id}&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>#{name}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/a<span style="font-weight: bold; color: black;">&gt;</span></span></span> (<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#yaps/#{my_id}/#{cat_id}&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>Our messages<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/a<span style="font-weight: bold; color: black;">&gt;</span></span></span>)<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;br</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span> /app:iterator<span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- more UI for viewing a dog --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;r:get.animal.response[type=cat] then show and bind[cat_info] else hide&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- UI for viewing a cat --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- another section of the app --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>So, when <tt>http://yourapp.com/pets#dog/fido</tt> loads, a remote message will be fired, a response (hopefully containing some friends) will return, the iterator will be executed, and links will be generated. If Fido has a cat friend named Milo, we&#8217;d get HTML generated (client-side), like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml">    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#cat/milo&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>Milo<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/a<span style="font-weight: bold; color: black;">&gt;</span></span></span> (<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#yaps/fido/milo&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>Our messages<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/a<span style="font-weight: bold; color: black;">&gt;</span></span></span>)</pre></div></div>

<p>Both of those links will fire an Appcelerator message when clicked, as well as changing the hash component of the URL (which is what makes forward/backward usable). Because we&#8217;ve set up an else-clause for the dog-ui section (<tt>r:get.animal.request[type=dog] then show and bind[dog_info] else hide</tt>), if the user clicks the <tt>#cat/milo</tt> link, the currect section will hide, and the cat-ui section will be shown.</p>
<p>So, with a few lines of declarative javascript, we&#8217;ve been able to retain all the webby goodness of deep-linking, and also reap the benefits of Appcelerator&#8217;s transparent client/server messaging and client-side templating. Cool, huh?</p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Deep-linking+into+RIAs&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fdeep-linking-into-rias.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/deep-linking-into-rias.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Appcelerator Protocol</title>
		<link>http://www.appcelerant.com/appcelerator-protocol.html</link>
		<comments>http://www.appcelerant.com/appcelerator-protocol.html#comments</comments>
		<pubDate>Tue, 02 Sep 2008 13:51:32 +0000</pubDate>
		<dc:creator>Amro Mousa</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Message Broker]]></category>
		<category><![CDATA[Protocol]]></category>
		<category><![CDATA[Service Broker]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/?p=79</guid>
		<description><![CDATA[The intent of this message is to provide a little clarification to those wanting to write their own Appcelerator backend (Service Broker). Appcelerator Service (server side) and Message (client side) Brokers use a simple, standard JSON protocol to send and respond to messages. Data is posted to the server via an Ajax request and the [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Appcelerator Protocol", url: "http://www.appcelerant.com/appcelerator-protocol.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><p>The intent of this message is to provide a little clarification to those wanting to write their own Appcelerator backend (Service Broker). Appcelerator Service (server side) and Message (client side) Brokers use a simple, standard <a title="YASON" href="http://www.json.org">JSON</a> protocol to send and respond to messages. Data is posted to the server via an Ajax request and the Service Broker first parses the request to find messages, which are mapped to some service handler, function, or method via the type (name) of the message. Next the Service Broker delegates the message to the handler to finish its computation and returns the result using the same protocol definition. More detail on the protocol definition is available <a title="ServiceBrokers" href="http://doc.appcelerator.org/reference/service_broker/index.html">here</a>, but the basic structure goes something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #66cc66;">&#123;</span>
    <span style="color: #3366CC;">&quot;timestamp&quot;</span>: <span style="color: #CC0000;">1214008045871</span>, 
    <span style="color: #3366CC;">&quot;version&quot;</span>: <span style="color: #3366CC;">&quot;1.0&quot;</span>, 
    <span style="color: #3366CC;">&quot;messages&quot;</span>: 
    <span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&#123;</span>
          <span style="color: #3366CC;">&quot;type&quot;</span>: <span style="color: #3366CC;">&quot;app.test.message.request&quot;</span>, 
          <span style="color: #3366CC;">&quot;scope&quot;</span>: <span style="color: #3366CC;">&quot;appcelerator&quot;</span>, 
          <span style="color: #3366CC;">&quot;version&quot;</span>: <span style="color: #3366CC;">&quot;1.0&quot;</span>, 
          <span style="color: #3366CC;">&quot;data&quot;</span>: <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;message&quot;</span>: <span style="color: #3366CC;">&quot;Hello,world&quot;</span><span style="color: #66cc66;">&#125;</span>
         <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>where messages is an array of messages. Each message is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #66cc66;">&#123;</span>
    <span style="color: #3366CC;">&quot;type&quot;</span>: <span style="color: #3366CC;">&quot;app.test.message.request&quot;</span>, 
    <span style="color: #3366CC;">&quot;scope&quot;</span>: <span style="color: #3366CC;">&quot;appcelerator&quot;</span>, 
    <span style="color: #3366CC;">&quot;version&quot;</span>: <span style="color: #3366CC;">&quot;1.0&quot;</span>, 
    <span style="color: #3366CC;">&quot;data&quot;</span>: <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;message&quot;</span>: <span style="color: #3366CC;">&quot;Hello,world&quot;</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>where type is the name of the message, scope describes which elements on the client will receive the message, and data is a JSON object containing the data payload of the message.</p>
<p>Astute readers might have gathered that the request/response procedure is synchronous. This decision was made after trying both polling and synchronous methods. Synchronous behavior was chosen for its efficiency and because it did not adversely impact the user experience on the client.</p>
<p>One final note: It&#8217;s important to set the content type in the http response to &#8220;application/json&#8221; so the client knows how to parse the incoming messages.</p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Appcelerator+Protocol&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fappcelerator-protocol.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/appcelerator-protocol.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE6 Transparent PNGs</title>
		<link>http://www.appcelerant.com/ie6-transparent-pngs.html</link>
		<comments>http://www.appcelerant.com/ie6-transparent-pngs.html#comments</comments>
		<pubDate>Tue, 03 Jun 2008 21:35:49 +0000</pubDate>
		<dc:creator>Hamed Hashemi</dc:creator>
				<category><![CDATA[Documentation]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/?p=60</guid>
		<description><![CDATA[One of the undocumented features of the Appcelerator WebSDK is the built-in support of transparent PNG images for IE6. A simple Google search will show you many, many different Javascript code snippets that you can put in your app to apply the PNG transparency filter on your images. But we thought we could provide this [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "IE6 Transparent PNGs", url: "http://www.appcelerant.com/ie6-transparent-pngs.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><p>One of the undocumented features of the Appcelerator WebSDK is the built-in support of transparent PNG images for IE6. A simple Google search will show you many, many different Javascript code snippets that you can put in your app to apply the PNG transparency filter on your images. But we thought we could provide this in our framework for you so you don&#8217;t have to worry about it.</p>
<p>The only problem has been that you couldn&#8217;t control exactly which images you want the filter to apply to. For example, if the filter applied to a div that had a background-image and a position, then potentially nothing inside that div would be clickable. And you also probably don&#8217;t want the extra processing on PNG images that don&#8217;t have any transparency to begin with.</p>
<p>So on the next released version of the WebSDK (2.1.3), we&#8217;ll have a .htc file that you can use in your CSS to control which images you want the PNG fix to apply to. For example you can do</p>

<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #6666ff;">.png</span> 
<span style="color: #66cc66;">&#123;</span>
   behavior<span style="color: #66cc66;">:</span> <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #933;">/images/iepngfix<span style="color: #6666ff;">.htc</span></span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>to apply on all elements that have the class &#8220;png&#8221; or you can do</p>

<div class="wp_syntax"><div class="code"><pre class="css">img, div 
<span style="color: #66cc66;">&#123;</span>
   behavior<span style="color: #66cc66;">:</span> <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #933;">/images/iepngfix<span style="color: #6666ff;">.htc</span></span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>to simply apply on all img and div elements.</p>
<p>Having these .htc files may not be the best thing in the world, but at least you can have control on where the PNG fix is applied.</p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=IE6+Transparent+PNGs&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fie6-transparent-pngs.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/ie6-transparent-pngs.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing an Appcelerator Plugin</title>
		<link>http://www.appcelerant.com/implementing-an-appcelerator-plugin.html</link>
		<comments>http://www.appcelerant.com/implementing-an-appcelerator-plugin.html#comments</comments>
		<pubDate>Tue, 04 Mar 2008 11:24:58 +0000</pubDate>
		<dc:creator>Andrew Zuercher</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/implementing-an-appcelerator-plugin.html</guid>
		<description><![CDATA[Purpose
The Appcelerator framework allows for the extension of the build and deployment process so that you can add components and modify the behavior of your service implementations.  It is quite powerful and not only restricted to services, plugins for example could span across to the modification of applications and the entire build and deployment [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Implementing an Appcelerator Plugin", url: "http://www.appcelerant.com/implementing-an-appcelerator-plugin.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><h3>Purpose</h3>
<p>The Appcelerator framework allows for the extension of the build and deployment process so that you can add components and modify the behavior of your service implementations.  It is quite powerful and not only restricted to services, plugins for example could span across to the modification of applications and the entire build and deployment process for that matter.  For simplicity, this article will identify how to create a new component added to our java service for monitoring performance.</p>
<h3>Install Appcelerator</h3>
<p>I&#8217;ve assumed that you&#8217;ve successfully downloaded and installed Appcelerator from <a href="http://www.appcelerator.org/products">http://www.appcelerator.org/products</a>.  Once you&#8217;ve gone through the installer you can get the java service</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">  app install:service java</pre></td></tr></table></div>

<p>You may want to get an existing plugin (I needed the java:spring plugin to implement my plugin):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">  app install:plugin java:spring</pre></td></tr></table></div>

<h3>Plugin Creation</h3>
<p>Lets assume that our plugin is going to be called java:perf, to create the plugin project:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict">  cd $HOME/workspace
  app create:plugin . java:perf</pre></td></tr></table></div>

<p>This will create a sub directory named java_perf, now you will want to create a proper license entry in your build.yml for example check out <a href="http://svn.appcelerator.org/appcelerator_plugins/java_perf/trunk/build.yml">build.yml</a>.  You now have the opportunity to modify the entry points in your <a href="http://svn.appcelerator.org/appcelerator_plugins/java_perf/trunk/src/java_perf.rb">java_perf.rb</a>.  I personally wanted to setup my install script so that:</p>
<ul>
<li> it dynamically builds the jar when we add the plugin to a project</li>
<li> modifies the spring.xml to include the bean entries for my component</li>
</ul>
<p>All of this is in before_add_plugin(event) callback.  If you want to see where this is called, check out <a href="http://svn.appcelerator.org/appcelerator_sdk/trunk/components/runtime/src/cmdline/commands/add_plugin.rb">add_plugin.rb</a>.  Once my coding is complete (added my source files to java_perf/src), I created the plugin distribution:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">rake zip</pre></td></tr></table></div>

<h3>Install the plugin</h3>
<p>As you noticed, I haven&#8217;t covered debugging the plugin just yet, this is because I want to first set up the plugin for installation and then work with it in my install directory.  Lets install it to the local machine:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">app install:plugin java_perf.zip</pre></td></tr></table></div>

<h3>Add the plugin to my project</h3>
<p>I thought that I&#8217;d create a blank java project real quick</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict">cd workspace
app create:project . myproject java</pre></td></tr></table></div>

<p>now we can add the spring plugin and our new java:perf plugin to our project</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict">cd myproject
app add:plugin java:spring
app add:plugin java:perf</pre></td></tr></table></div>

<p>You can now directly modify the java_perf.rb file in your install directory</p>
<ul>
<li> windows: c:/Program Files/Appcelerator/releases/plugin/java_perf/1.0</li>
<li> unix: /usr/local/Appcelerator/releases/plugin/java_perf/1.0</li>
<li> mac os: /Library/Appcelerator/releases/plugin/java_perf/1.0</li>
</ul>
<p>I suggest that you continue to modify the java_perf.rb this way to debug it and then finalize by copying back over to $HOME/workspace/java_perf</p>
<h3>Next Steps</h3>
<p>As a final parting you can release your new plugin so that others may be able to leverage it</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict">cd $HOME/workspace/java_perf
app release java_perf.zip</pre></td></tr></table></div>

<p>-andrew</p>
<p>this article is cross posted at:<br />
<a href="http://zuerchtech.com/2008/3/4/implementing-an-appcelerator-plugin">http://zuerchtech.com/2008/3/4/implementing-an-appcelerator-plugin</a></p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Implementing+an+Appcelerator+Plugin&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fimplementing-an-appcelerator-plugin.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/implementing-an-appcelerator-plugin.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Effectively Implementing Appcelerator RIAs and Services</title>
		<link>http://www.appcelerant.com/effectively-implementing-appcelerator-rias-and-services.html</link>
		<comments>http://www.appcelerant.com/effectively-implementing-appcelerator-rias-and-services.html#comments</comments>
		<pubDate>Tue, 26 Feb 2008 06:53:47 +0000</pubDate>
		<dc:creator>Andrew Zuercher</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/effectively-implementing-appcelerator-rias-and-services.html</guid>
		<description><![CDATA[Purpose
If you are venturing to implement a Rich Internet Application (RIA) with Appcelerator, then there are features about the framework that I&#8217;m really geared up about that help you implement your solution effectively.  When used together these items enable the agile creation of solutions in a shorter time that integrate nicely with services in [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Effectively Implementing Appcelerator RIAs and Services", url: "http://www.appcelerant.com/effectively-implementing-appcelerator-rias-and-services.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><h3>Purpose</h3>
<p>If you are venturing to implement a Rich Internet Application (RIA) with Appcelerator, then there are features about the framework that I&#8217;m really geared up about that help you implement your solution effectively.  When used together these items enable the agile creation of solutions in a shorter time that integrate nicely with services in an existing architecture and provide a richer user experience for clients.</p>
<h3>Achieving Re-use</h3>
<p>There are a bunch of ways to do this, personally I leverage app:content files judiciously.  You can provide args in the definition of your content files so that you can use the same content file over again.  Some things to keep in mind:</p>
<ul>
<li>reference the args as #{argname} in your element ids, any variables, and make sure that functions you call take this into account (possibly make them stateless)</li>
<li>the order in which the content file is loaded to make sure that listeners are properly setup to respond to messages</li>
</ul>
<p>here is an example app:content reference</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;app:content</span> <span style="color: #000066;">lazy</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;mainstate[person] then show else hide&quot;</span> 
  <span style="color: #000066;">args</span>=<span style="color: #ff0000;">&quot;{'prefix':'person'}&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/app:content<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></td></tr></table></div>

<p>here is the content.html file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#{prefix}header&quot;</span> ...
<span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></pre></td></tr></table></div>

<p>I know that this may be abstract, but the point is that you can use the arg &#8220;prefix&#8221; to reuse the file and provide alternative behavior based on it.</p>
<h3>Mocking Services</h3>
<p>You can mock out server side services extremely easily with the framework.  This is extremely useful whether your goal is:</p>
<ul>
<li>Visual Use Cases: implementing a prototype that results in 100% useable application code</li>
<li>Working offline: no need to run a web server at all, you can directly run off of a simple file</li>
</ul>
<p>Once you have this in place, you have a well defined contract in place which makes knocking out the service implementation a snap.</p>
<h3>No restart</h3>
<p>If you are just working on your application (no service work), you can directly make changes to your application without requiring rebuilding and redeploying your application.  In java I typically work directly in my tomcat deploy directory and have an ant task to pull the web files into my workspace.  With our next version of the framework coming out, this will be even easier with our support for Jetty coming and the Ruby based command line tool.  In the meantime, the following is useful in your java project:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml">ant pullweb</pre></td></tr></table></div>

<p>Make sure that you have your deploy.dir setup in your $HOME/.ant.properties for example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml">deploy.dir=/Applications/apache-tomcat-6.0.14/webapps</pre></td></tr></table></div>

<p>With our Ruby implementation for example, this is even easier (running webbrick directly in your workspace).</p>
<h3>Great Support for Model Objects</h3>
<p>I&#8217;ve touched a bit on this in a previous post on <a href="http://www.appcelerant.com/json-serialization-with-appcelerator-java-services.html"><span class="caps">JSON</span> serialization with Appcelerator java services</a>.  To summarize the framework enables</p>
<ul>
<li>Hibernate persistence: ModelObjects can easily be used with our Hibernate integration</li>
<li><span class="caps">JSON</span> serialization: Serializing and deserialing complex objects is a snap</li>
</ul>
<h3>Slick support for forms</h3>
<p>In case you haven&#8217;t looked, you dont need to include a form tag anywhere in the application: simply add a fieldset attribute to your input elements and put something like this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;age&quot;</span> <span style="color: #000066;">fieldset</span>=<span style="color: #ff0000;">&quot;save_person&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">fieldset</span>=<span style="color: #ff0000;">&quot;save_person&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;input</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Create Person&quot;</span> <span style="color: #000066;">fieldset</span>=<span style="color: #ff0000;">&quot;save_person&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  on=&quot;click then r:create.person.request&quot; type=&quot;button&quot;/&gt;</pre></td></tr></table></div>

<p>This will create a message of type r:create.person.request with the payload being all your input elements with fieldset of &#8220;save_person&#8221; ex:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript"><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'name'</span>:<span style="color: #3366CC;">'jim'</span>,<span style="color: #3366CC;">'age'</span>:<span style="color: #3366CC;">'35'</span><span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>To me, this is very eloquent solution to leveraging form input elements with a messaging architecture.</p>
<h3>Easy to use UI widgets</h3>
<p>I love some of the widgets that are at your fingertips.  Some of the ones that i use the most</p>
<ul>
<li>app:panel: slick styling</li>
<li>app:datatable: great way to show rows of data</li>
<li>app:chart: fantastic visual effects to bring life to data</li>
</ul>
<p>All these widgets have a very terse usage that is very intuitive.  For example, using this set, I was able to knock out a realtime dashboard application in 3 evenings.</p>
<h3>Service Platform Options</h3>
<p>Pitching the right framework is something that varies from project to project.  For example, possibly your client wants to leverage an existing java infrastructure and is sold on its robustness for scaling enterprise applications, while on the other side possibly you want to take advantage of using a Rails controller and <span class="caps">ORM</span>/Active Record implementation for rapid development.  On the same level you also have the option for implementing services to customers that deliver in a .Net solution architecture.  Many other platforms also exist (python, <span class="caps">PHP</span>) enabling the opportunity to plug into those services as well.</p>
<p>The app:get non-visual widget also makes pre-existing services accessible as well and since Messaging is at the heart of the implementation, plugging into an <span class="caps">ESB</span> or pure <span class="caps">MOM</span> framework is perfectly setup as well.</p>
<h3>Wrap Up</h3>
<p>In this article 7 key tips have been covered</p>
<ul>
<li>re-use</li>
<li>mock services</li>
<li>no restart</li>
<li>model objects</li>
<li>messaging &#38; forms</li>
<li>UI widgets</li>
<li>platform options</li>
</ul>
<p>Although there are more items that have not been covered here, these are the ones that appeal personally to me and have noticed a huge gain in doing much more with less effort.</p>
<p>
this article cross-posted at <a href="http://blog.zuerchtech.com/2008/02/26/effectively-implementing-appcelerator-rias-and-services/">http://blog.zuerchtech.com/2008/02/26/effectively-implementing-appcelerator-rias-and-services/</a></p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Effectively+Implementing+Appcelerator+RIAs+and+Services&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Feffectively-implementing-appcelerator-rias-and-services.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/effectively-implementing-appcelerator-rias-and-services.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Appcelerator Applications</title>
		<link>http://www.appcelerant.com/debugging-appcelerator-applications.html</link>
		<comments>http://www.appcelerant.com/debugging-appcelerator-applications.html#comments</comments>
		<pubDate>Thu, 21 Feb 2008 04:05:29 +0000</pubDate>
		<dc:creator>Andrew Zuercher</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/debugging-appcelerator-applications.html</guid>
		<description><![CDATA[Purpose
The ability to debug applications is critical when things go sideways on you.  I understand that for some of the developers out there learning a new framework can be challenging.  Appcelerator provides developers with the tool-set to drastically reduce the amount of javascript code that they need to write and to be honest [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Debugging Appcelerator Applications", url: "http://www.appcelerant.com/debugging-appcelerator-applications.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><h3>Purpose</h3>
<p>The ability to debug applications is critical when things go sideways on you.  I understand that for some of the developers out there learning a new framework can be challenging.  Appcelerator provides developers with the tool-set to drastically reduce the amount of javascript code that they need to write and to be honest I think that the more you use the framework, the less you will need to debug applications as you familiarize yourself with the syntax.</p>
<h3>Alerts</h3>
<p>Prior to logging, alerts were the best way to display state during execution (showing you how old school I&#8217;ve been).  Nonetheless, sometimes this is just easier since it blocks execution until you confirm.  When you do this, you may want want to display objects.  Lets take for example the following</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">alert(myobject);</pre></td></tr></table></div>

<p>You make get an alert that shows:</p>
<p><a href="http://www.appcelerant.com/wp-content/uploads/2008/02/alert_object.png" title="alert_object.png"><img src="http://www.appcelerant.com/wp-content/uploads/2008/02/alert_object.png" alt="alert_object.png" /></a></p>
<p>Well that isn&#8217;t very useful.  If you want a nice string representation, use the following</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">alert(Object.toJSON(myobject));</pre></td></tr></table></div>

<p>Now you&#8217;ll get a nice JSON representation.</p>
<p><a href="http://www.appcelerant.com/wp-content/uploads/2008/02/alert_json.png" title="alert_json.png"><img src="http://www.appcelerant.com/wp-content/uploads/2008/02/alert_json.png" alt="alert_json.png" /></a></p>
<h3>Logging</h3>
<p>Safari and Firefox both provide you with the ability to view logging in the console.  For firefox, you need to install <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">firebug</a>.  If you are a safari user, you can use <a href="http://webkit.org/">webkit</a> (from the Debug menu, click &#8220;Show Javascript Console&#8221;).  Both of which make available a console that displays log statements.</p>
<p>In firefox you can do the following in javascript</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">console.log(&quot;wassup&quot;);</pre></td></tr></table></div>

<p>I suggest however that you dont do that!  Instead make use of the abstracted Logger</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict">Logger.info(&quot;wassup&quot;);</pre></td></tr></table></div>

<p>This will not only show up in Firefox, but also in Safari&#8217;s console.</p>
<p><a href="http://www.appcelerant.com/wp-content/uploads/2008/02/wassup.png" title="wassup.png"><img src="http://www.appcelerant.com/wp-content/uploads/2008/02/wassup.png" alt="wassup.png" /></a></p>
<p>On another note, you can use the debug=1 parameter in your URL and you will see a ton of appcelerator messages (ex: http:localhos:8080/myapp?debug=1).  I will caution you that it will almost look like spam given the amount of detail that is logged, but nonetheless its there if you want it.</p>
<p>You can also expand the Get and Post statements in firefox which will let you know what you have sent in your request and the JSON result that was returned from the server.</p>
<p><a href="http://www.appcelerant.com/wp-content/uploads/2008/02/ajaxlog.png" title="ajaxlog.png"><img src="http://www.appcelerant.com/wp-content/uploads/2008/02/ajaxlog.png" alt="ajaxlog.png" width="650" height="196" /></a></p>
<h3>In-line Debugging</h3>
<p>Lets say that you have an error in your app:script.  Well in Firefox, you can take a look at the console and that will give you some pretty good feedback, however its going to be some cryptic text and you can&#8217;t step through it.  You have 2 options that will enable you to step through the code:</p>
<ul>
<li>can make a call to a javascript function from your app:script (I normally define a project specific javascript file and put my functions in it)</li>
<li>define a $MQL in your javascript file &#8211; app:script goes away</li>
</ul>
<p>Here is scenario 1:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict"><span style="color: #009900;">&lt;app:script on<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;r:someevent.response then execute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  Myapp.dosomething(this.data);
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #66cc66;">/</span>app:script&gt;</span></pre></td></tr></table></div>

<p>and the corresponding javascript</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html4strict">Myapp = Class.create();
Myapp.dosomething = function(data)
{
  //code here
}</pre></td></tr></table></div>

<p>For scenario 2: alternatively you could just define in your javascript file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict">$MQL('r:someevent.response',function(type,msg,datatype,from)
{
	//code here
});</pre></td></tr></table></div>

<p>The downside of this is that your implementation is not colocated in a single html file, but the upside is that you can debug it.  Note that if you are not using content files, then you could alternatively define the above in a script element instead (a little cleaner), however if its a large app, then you really will want to decompose your app and use content files.</p>
<h3>IE Specific Debugging</h3>
<p>To be honest I&#8217;m not a huge IE fan, but it makes up an install base of 80% of the market.  Chances are that pretty much everyone out there is using IE if you are building an application that appeals to the general public.  To this end, Microsoft has made the .Net Developer Studio available for debugging your applications.  The steps are pretty simple:</p>
<ul>
<li>install .Net Studio and create a project</li>
<li>add your javascript files to your project</li>
<li>start IE with your app&#8217;s url</li>
<li>in Visual studio, select debug process and select your iexplorer.exe (sometimes it wont attach, but just keep trying and you should get there eventually)</li>
</ul>
<p>For understanding styling, I suggest downloading the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&amp;DisplayLang=en">Internet Explorer Developer Toolbar</a>.  This will allow you to inspect individual elements (using the hover) and then setting styling attributes on your elements.  This is much better than changing your html and reloading your page, especially for large applications.</p>
<h3>Firefox (Firebug) Specific debugging</h3>
<p>As with IE, you can use the firefox plugin to step through your code.</p>
<ul>
<li>load your app</li>
<li>open firebug</li>
<li>click script and select the html or javascript file and set your breakpoints</li>
</ul>
<p>As with IE, using the Inspect module in firebug is very useful for styling your application.</p>
<h3>Wrap Up</h3>
<p>In this article we covered a bunch of ways that you can get down to the details in your application</p>
<ul>
<li>alerts (and displaying objects as strings)</li>
<li>logging (appcelerator debug mode, custom log statements, ajax payloads)</li>
<li>in line javascript debugging and styling</li>
</ul>
<p>For many this may be an obvious repeat of the mechanisms that they currently use as web developers, but hopefully it will uncover some of the tactics that are used.</p>
<p>-andrew</p>
<p>this blog is cross posted at:<br />
<a href="http://zuerchtech.com/2008/2/21/debugging-appcelerator-applications">http://zuerchtech.com/2008/2/21/debugging-appcelerator-applications</a></p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Debugging+Appcelerator+Applications&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fdebugging-appcelerator-applications.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/debugging-appcelerator-applications.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Performance Tuning Appcelerator Applications</title>
		<link>http://www.appcelerant.com/performance-tuning-appclerator-applications.html</link>
		<comments>http://www.appcelerant.com/performance-tuning-appclerator-applications.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 05:25:57 +0000</pubDate>
		<dc:creator>Andrew Zuercher</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/performance-tuning-appclerator-applications.html</guid>
		<description><![CDATA[Purpose
Once you have developed a functional application and are priming for a roll-out, it will be in your interest to tune your application and identify those touch-points which are candidates for tuning.  I&#8217;ve preferred taking a quantitative approach to performance tuning so that you can compare metrics before and afterwards to identify improvement from [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Performance Tuning Appcelerator Applications", url: "http://www.appcelerant.com/performance-tuning-appclerator-applications.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><h3>Purpose</h3>
<p>Once you have developed a functional application and are priming for a roll-out, it will be in your interest to tune your application and identify those touch-points which are candidates for tuning.  I&#8217;ve preferred taking a quantitative approach to performance tuning so that you can compare metrics before and afterwards to identify improvement from a measurable perspective.  To this end, it is possible to record statistics both client and server side (currently only for java services).  Sometimes the culprit may be server side or client side and determining the area where performance bottlenecks exist is important which is why taking on a triangulation approach is important.</p>
<h3>Client Side</h3>
<p>Currently in the client, you can invoke the Appcelerator.Util.Performance component.  To enable it, simply include logStats=1 in your url, for example: http://localhost:8080/myapp?logStats=1.  The performance component by design will have no impact on your application unless you include this parameter.  By default, the servicebroker will calculate the statistics for handling all message types both local and remote.  The statistics will include the following:</p>
<ul>
<li>last &#8211; the last recorded time</li>
<li>mean</li>
<li>hits &#8211; aka count</li>
<li>min</li>
<li>max</li>
<li>total &#8211; total time overall</li>
</ul>
<p>These statistics will be logged to the console (available in Firefox with Firebug and in Safari with Webkit).</p>
<p><a href="http://www.appcelerant.com/wp-content/uploads/2008/02/picture-15.png" title="logging output"><img src="http://www.appcelerant.com/wp-content/uploads/2008/02/picture-15.png" alt="logging output" /></a></p>
<p>However, you do not need to have logging available to inspect the statistics (please see the section &#8220;Viewing Client Side statistics&#8221; below). In addition the component also allows for resetting all statistics to restart your recording.</p>
<h3>Capturing your own statistics</h3>
<p>If for some reason you want to capture statistics at a finer level, you can easily to this in javascript:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict">var stat = Appcelerator.Util.Performance.createStat();
//do some work
Appcelerator.Util.Performance.endStat(stat,&quot;mystat&quot;,&quot;mycategory&quot;);</pre></td></tr></table></div>

<p>In this example we create a statistic called &#8220;mystat&#8221; that will be included in a category of &#8220;mycategory&#8221;.  This is useful for filtering through your stats as well as being able to compare statistics side by side.  For example, the service broker will automatically record the following categories:</p>
<ul>
<li>remote</li>
<li>local</li>
</ul>
<h3>Viewing Client Side statistics</h3>
<p>You may want to include the following content file in your application and make it visible when the logStats=1 parameter is included.</p>
<p><a href="http://www.appcelerant.com/wp-content/uploads/2008/02/picture-13.png" title="performance screenshot"><img src="http://www.appcelerant.com/wp-content/uploads/2008/02/picture-13.png" alt="performance screenshot" /></a></p>
<p>Here is the content source itself<br />
<a href="http://www.appcelerant.com/wp-content/uploads/2008/02/clientperformancehtml.txt" title="clientperformance content file">clientperformance.html</a></p>
<h3>Tuning Considerations</h3>
<p>As you develop larger applications, it may be useful to consider the following tips:</p>
<ul>
<li>use lazy=true for app:content files.  this will help to delay long initial load times for your applications</li>
<li>if you do not lazily load your content files, consider postponing app:message invocations until they are needed</li>
<li>consider using CSS selectors in the implementation of your app:iterator rather than re-firing messages for logic</li>
<li>leverage the app:datacache to reduce the number of outgoing messages</li>
<li>make use of pagination if rendering large datasets &#8211; the app:datatable allows for client side pagination for example</li>
</ul>
<h3>Wrap Up</h3>
<p>We covered in this article</p>
<ul>
<li>how you can capture statistics for client side applications</li>
<li>some performance tips in regards to client applications</li>
</ul>
<p>In a later article we will identify how you can take this a step further to capture statistics on the server side as well with java services.This article has been cross posted on</p>
<li><a href="http://zuerchtech.com/2008/2/20/performance-tuning-appcelerator-applications">http://zuerchtech.com/2008/2/20/performance-tuning-appcelerator-applications</a></li>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Performance+Tuning+Appcelerator+Applications&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fperformance-tuning-appclerator-applications.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/performance-tuning-appclerator-applications.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JSON serialization with appcelerator java services</title>
		<link>http://www.appcelerant.com/json-serialization-with-appcelerator-java-services.html</link>
		<comments>http://www.appcelerant.com/json-serialization-with-appcelerator-java-services.html#comments</comments>
		<pubDate>Fri, 08 Feb 2008 06:44:50 +0000</pubDate>
		<dc:creator>Andrew Zuercher</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[JSON java service appcelerator]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/json-serialization-with-appcelerator-java-services.html</guid>
		<description><![CDATA[Using Model Objects
The issue of serializing/transforming model objects is not new, heck I&#8217;ve been doing this for quite some time:

rmi (ejb/corba)
xml (jms, soap, etc..)
json

JSON is not the only way to serialize objects for web2.0 applications, but its the most abundant and heavily used throughout our framework.  Doing this is actually really easy to do [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "JSON serialization with appcelerator java services", url: "http://www.appcelerant.com/json-serialization-with-appcelerator-java-services.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><h3>Using Model Objects</h3>
<p>The issue of serializing/transforming model objects is not new, heck I&#8217;ve been doing this for quite some time:</p>
<ul>
<li>rmi (ejb/corba)</li>
<li>xml (jms, soap, etc..)</li>
<li>json</li>
</ul>
<p>JSON is not the only way to serialize objects for web2.0 applications, but its the most abundant and heavily used throughout our framework.  Doing this is actually really easy to do with appcelerator IModelObejects.  Our IModelObjects can easily be used along with hibernate for persistance, but lets leave that for later for now.  When you define your Model classes, there are some very simple things to keep in mind:</p>
<ul>
<li>annotate your attributes with @MessageAttr</li>
<li>have your class implement IModelObject</li>
</ul>
<p>Here is a simple example:</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
15
16
</pre></td><td class="code"><pre class="html4strict">public class User implements IModelObject, Serializable {
  private static final long serialVersionUID = 1L;
&nbsp;
  @MessageAttr
  public String name;
&nbsp;
  public void setUsername(String username) {
    this.username = username;
  }
&nbsp;
 public void setPassword(String password) {
   this.password = password;
  }
&nbsp;
  ...
}</pre></td></tr></table></div>

<h3>Unit test it</h3>
<p>Now to test the rendering of our object to JSON with a simple junit test&#8230;.  As you can see, we leverage the Appcelerator framework to serialize our objects to JSON</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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="html4strict">public class ForumTest extends TestCase { 
&nbsp;
public void testSimple() {
  User user = createUser();
  Message message = new Message();
  JSONMessageDataObject data = new JSONMessageDataObject();
  message.setData(data);
  message.getData().put(&quot;user&quot;, user);
  message.getData().put(&quot;count&quot;, 1);
  String messagestr = data.toDataString();
  assertEquals(messagestr,&quot;\&quot;user\&quot;:{\&quot;password\&quot;:\&quot;pwd\&quot;,\&quot;threads\&quot;:0,\&quot;fullName\&quot;
  :\&quot;antewew\&quot;,\&quot;username\&quot;:\&quot;azuercher\&quot;,\&quot;state\&quot;:\&quot;mystate\&quot;,\&quot;email\&quot;:\&quot;email\&quot;,
  \&quot;posts\&quot;:0,\&quot;id\&quot;:0},\&quot;count\&quot;:1}&quot;);
}
&nbsp;
private User createUser() {
  User user = new User();
  user.setEmail(&quot;email&quot;);
  user.setFullName(&quot;ante wew&quot;);
  user.setId(new Long(0));
  user.setPassword(&quot;pwd&quot;);
  user.setPosts(new Long(0));
  user.setState(&quot;mystate&quot;);
  user.setThreads(new Long(0));
  user.setUsername(&quot;azuercher&quot;);
  return user;
}
&nbsp;
}</pre></td></tr></table></div>

<h3>Dealing with recursion</h3>
<p>Whats always a bit of a tangle is understanding how to deal with the recursive/circular relationship.<br />
If you take a look at JSONObject you will see 2 overridden methods for createBean</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict">public static JSONObject createBean(IModelObject object,MessageAttr parentAtt, String context,String[] parentSuppres,int level, int maxlevels)
public static JSONObject createBean(IModelObject object)</pre></td></tr></table></div>

<p>The latter is obviously a bit more simple, but the former is where the power is.  In the MessageAttr annotation, you can provide the suppress attribute which is a comma separated list of aggregates (using bean.name notation to not serialize).  This is used for attributes in your IModelObject implementation where the association is with another IModelObject. The following model is for a forum object model where the following aggregate hierarchy exists:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict">* Forum
** ForumThread
*** Post</pre></td></tr></table></div>

<p>In the snippet below, I&#8217;ve omitted the getter/setter methods for the aggregates for simplicity.</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
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="html4strict">public class User implements IModelObject, Serializable {
  @MessageAttr (suppress=&quot;user,thread.lastPost&quot;)
  public Post lastPost;
}
&nbsp;
public class Post implements IModelObject, Serializable {
  @MessageAttr(suppress=&quot;lastPost,forum.lastPost&quot;)
  public Forumthread thread;
&nbsp;
  @MessageAttr (suppress=&quot;lastPost&quot;)
  public User user;
}
&nbsp;
public class Forumthread implements IModelObject, Serializable {
  @MessageAttr (suppress=&quot;lastPost&quot;)
  public Forum forum;
&nbsp;
  @MessageAttr (suppress=&quot;thread,user.lastPost&quot;)
  public Post lastPost;
}
&nbsp;
public class Forum implements IModelObject, Serializable {
  @MessageAttr (suppress=&quot;thread.forum,thread.lastPost,user.lastPost&quot;)
  public Post lastPost;
&nbsp;
}</pre></td></tr></table></div>

<h3>Rolling your own serialization</h3>
<p>Assuming you know what your JSON string is going to look like, you can use our RawMessageDataList and RawMessageDataObject to serialze your objects.  This is pretty useful if you already are rendering JSON in an existing framework and don&#8217;t want to have to transform to and back again.  The snippet below shows with static strings just so that you get the idea:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html4strict">IMessageDataList people = new RawMessageDataList(
&quot;[{'name':'joe','age':22},{'name':'jane','age':33}]&quot;);
&nbsp;
IMessageDataList dog = new RawMessageDataObject(&quot;{'breed':'doberman','weight':78}&quot;);
&nbsp;
Message message = new Message();
message.setData(new JSONMessageDataObject());
message.getData().put(&quot;people&quot;, people);
message.getData().put(&quot;dog&quot;, dog);</pre></td></tr></table></div>

<h3>Using Coarse Grained objects</h3>
<p>This is probably the simplest/prettiest way to implement your services assuming that you aren&#8217;t interested in using fine grained classes that are associated with most of today&#8217;s persistence frameworks.  Here is how you would create a single compound JSON object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html4strict">IMessageDataObject dog = MessageUtils.createMessageDataObject();
obj.put(&quot;breed&quot;,&quot;doberman&quot;);
obj.put(&quot;wieght&quot;,78);Message message = new Message();
message.setData(new JSONMessageDataObject());
message.getData().put(&quot;dog&quot;, dog);</pre></td></tr></table></div>

<p>and now with a collection:</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
15
16
17
</pre></td><td class="code"><pre class="html4strict">IMessageDataList<span style="color: #ddbb00;">&amp;lt;</span>IMessageDataObject<span style="color: #ddbb00;">&amp;gt;</span> people =
MessageUtils.createMessageDataObjectList();
&nbsp;
IMessageDataObject joe = MessageUtils.createMessageDataObject();
joe.put(&quot;name&quot;,&quot;joe&quot;);
joe.put(&quot;age&quot;,22);
&nbsp;
IMessageDataObject jane = MessageUtils.createMessageDataObject();
joe.put(&quot;name&quot;,&quot;jane&quot;);
joe.put(&quot;age&quot;,33);
&nbsp;
people.put(joe);
people.put(jane);
&nbsp;
Message message = new Message();
message.setData(new JSONMessageDataObject());
message.getData().put(&quot;people&quot;, people);</pre></td></tr></table></div>

<h3>Summary</h3>
<p>As you can see there are quite a bit of alternatives for you based on what your needs are to accommodate your service implementations.  I&#8217;ve personally used all of the above as I&#8217;ve implemented:</p>
<ul>
<li>Model Objects: using hibernate for persistence</li>
<li>Custom Serialization: for integrating with pre-rendered objects (commons-monitoring)</li>
<li>Coarse Grained: in implementing a dashboard/event driven solution</li>
</ul>
<p>cheers and enjoy,</p>
<p>-andrew</p>
<p>cross-posted @ <a href="http://zuerchtech.com/2008/2/8/json-serialization-with-appcelerator-java-services" target="_blank">http://zuerchtech.com/2008/2/8/json-serialization-with-appcelerator-java-services</a></p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=JSON+serialization+with+appcelerator+java+services&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fjson-serialization-with-appcelerator-java-services.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/json-serialization-with-appcelerator-java-services.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building Appcelerator Widgets</title>
		<link>http://www.appcelerant.com/building-appcelerator-widgets.html</link>
		<comments>http://www.appcelerant.com/building-appcelerator-widgets.html#comments</comments>
		<pubDate>Fri, 28 Dec 2007 16:16:05 +0000</pubDate>
		<dc:creator>Tejus Parikh</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Example]]></category>

		<guid isPermaLink="false">http://www.appcelerant.com/building-appcelerator-widgets.html</guid>
		<description><![CDATA[Extending Appcelerator to support new widgets is actually pretty straight-forward. The first, and quite possibly most important, step is to create a name.  The name must be unique to its namespace and, unless you want all your hard work to be ignored, be somewhat descriptive of what it does.  If it&#8217;s a standard, [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Building Appcelerator Widgets", url: "http://www.appcelerant.com/building-appcelerator-widgets.html" });</script>]]></description>
			<content:encoded><![CDATA[<p></p><p>Extending Appcelerator to support new widgets is actually pretty straight-forward. The first, and quite possibly most important, step is to create a name.  The name must be unique to its namespace and, unless you want all your hard work to be ignored, be somewhat descriptive of what it does.  If it&#8217;s a standard, general use widget, like a button, panel, or script block, it makes sense to put it in the <code>app</code> namespace.  Specialized widgets, such as the widget to drop in google analytics, go in their own namespace. Staying within these conventions makes it simpler to include new widgets into the Appcelerator core.  Other than convention, neither the name nor namespace will effect the functionality of the widget.  For the stopwatch widget, we picked <code>app:stopwatch</code>.  The rest of this article focuses on how we built it.</p>
<p>Once you&#8217;ve got a name, it’s time to create the directory structure for the widget.  Navigate to the modules directory in the appcelerator source in your file-browser of choice, and create the following directory structure:<br />
<img src="http://www.appcelerant.com/wp-content/uploads/2007/12/directory-structure.png" alt="Directory Structure" /><br />
The directory should be called <code>%namespace%_%widget%</code> and needs to contain a file called <code>%namespace%_%widget%.js</code>. The directories css, images, and js aren&#8217;t required, you can omit them if your module does need any css, images nor depend on any javascript not contained within the widget.  The stopwatch does not need any third party javascript or images, so we’re going to remove those directories.</p>
<p>You can write out all the boiler plate on your own, or you can do what we do and copy from a pre-existing widget.  <code>app:script</code> is a good choice, since its implementation is relatively simple.<br />
Lets take a look at the structure of the template:</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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="javascript">Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Stopwatch</span> =
<span style="color: #66cc66;">&#123;</span>
	getName: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'appcelerator stopwatch'</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getDescription: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'stopwatch widget'</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getVersion: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1.0</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getSpecVersion: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1.0</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getAuthor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'My Name'</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getModuleURL: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'http://www.appcelerator.org'</span>;
	<span style="color: #66cc66;">&#125;</span>,
	isWidget: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getWidgetName: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'app:stopwatch'</span>;
	<span style="color: #66cc66;">&#125;</span>,
	getAttributes: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//these attributes become part of the parameter map.</span>
	<span style="color: #66cc66;">&#125;</span>,
	getActions: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//custom actions that this widget responds to go here</span>
	<span style="color: #66cc66;">&#125;</span>,
	compileWidget: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>params<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//put code here if you need to add run-time code</span>
	<span style="color: #66cc66;">&#125;</span>,
	buildWidget: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>element,parameters<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//this is where you build your widget’s html code.</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #3366CC;">'position'</span> : Appcelerator.<span style="color: #006600;">Compiler</span>.<span style="color: #006600;">POSITION_REMOVE</span>
		<span style="color: #66cc66;">&#125;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
Appcelerator.<span style="color: #006600;">Core</span>.<span style="color: #006600;">registerModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'stopwatch'</span>,Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Panel</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Boring Functions</strong><br />
<code>getName</code>, <code>getDescription</code>, <code>getVersion</code>, <code>getAuthor</code>, and <code>getModuleURL</code>, are all for documentation.   They do not effect the functionality of your widget.</p>
<p><code>getSpecVersion</code> is the version for the module framework.  For now, and the foreseeable future, it&#8217;s 1.0.  This will be used in future versions of Appcelerator to provide backwards compatibility.</p>
<p>Again, in the future modules might be more than just widgets.  The function <code>isWidget</code> should return true for widgets, so that the Appcelerator compiler does the right thing.</p>
<p>All of these functions are already correctly filled out in the stub above.</p>
<p><strong>getAttributes: function()</strong><br />
this method returns the list of supported element attributes.  The attributes defined in here will become the parameter keys in the parameter map passed into buildWidget and compileWidget.  Each item in the list should be an object with the following properties:</p>
<table class="properties">
<tr>
<th>name</th>
<th>description</th>
<th>required</th>
</tr>
<tr>
<td>name</td>
<td>the name of the attribute</td>
<td>true</td>
</tr>
<tr>
<td>optional</td>
<td>designates wether this is a required attribute</td>
<td>true</td>
</tr>
<tr>
<td>description</td>
<td>a short description of the attribute</td>
<td>true</td>
</tr>
<tr>
<td>defaultValue</td>
<td>the default value if the user does not suppy a value</td>
<td>true</td>
</tr>
</table>
<p>The stopwatch has one optional parameter to control wether or not the buttons should be displayed.  The complete code for this function in the stopwatch widget is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript">getAttributes: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #000066;">name</span>: <span style="color: #3366CC;">'show_button'</span>, optional: <span style="color: #003366; font-weight: bold;">true</span>, description: <span style="color: #3366CC;">&quot;Set to false to hide button&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span>,</pre></td></tr></table></div>

<p><strong>buildWidget: function(element, parameters)</strong><br />
This is where you define the presentation for your widget.  Only presentation logic should reside in this function.  Things like state and event listeners go in the <code>compileWidget</code> function described later.  </p>
<p>The stopwatch widget replaces &lt;app:stopwatch&gt;&lt;/app:stopwatch&gt; with the text for the current time and, optionally, some buttons to control the widget.   Therefore, we need to construct an html string.</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
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> html = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;'</span> + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'&quot; class=&quot;stopwatch&quot;&gt;'</span><span style="color: #66cc66;">&#41;</span>;
html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;table&gt;&lt;tr&gt;'</span><span style="color: #66cc66;">&#41;</span>;
html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;td&gt;&lt;h3 id=&quot;'</span> + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'_clock&quot;&gt;00:00 &lt;span&gt;00&lt;/span&gt;&lt;/h3&gt;&lt;/td&gt;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>parameters<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">&quot;show_button&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #3366CC;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#123;</span>
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;td class=&quot;stopwatch_start&quot;&gt;'</span><span style="color: #66cc66;">&#41;</span>
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;app:button width=&quot;100&quot; on=&quot;click then script[Appcelerator.Module.Stopwatch.start_stop(<span style="color: #000099; font-weight: bold;">\'</span>'</span> 
            + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>)]&quot;&gt;Start&lt;/app:button&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;/td&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;td class=&quot;stopwatch_stop&quot;&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;app:button width=&quot;100&quot; on=&quot;click then script[Appcelerator.Module.Stopwatch.start_stop(<span style="color: #000099; font-weight: bold;">\'</span>'</span> 
            + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>)] and l:'</span> + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'_reset&quot;&gt;Stop&lt;/app:button&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;/td&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;td class=&quot;stopwatch_reset&quot;&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;app:button disabled=&quot;true&quot; width=&quot;100&quot; on=&quot;click then script[Appcelerator.Module.Stopwatch.clear_time(<span style="color: #000099; font-weight: bold;">\'</span>'</span>
            + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>)] and disable or l:'</span> + element.<span style="color: #006600;">id</span> + <span style="color: #3366CC;">'_reset then enable&quot;&gt;Reset&lt;/app:button&gt;'</span><span style="color: #66cc66;">&#41;</span>;
    html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;/td&gt;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;/tr&gt;&lt;/table&gt;'</span><span style="color: #66cc66;">&#41;</span>;
html.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>As you can see, this is pretty basic stuff. It&#8217;s just html.  You’ll notice that we’ve used another widget <code>app:button</code> in this widget.  This is completely valid and highly recommended.  After all, it does not make sense to reinvent the wheel, if you don’t have to.  </p>
<p>Also, notice that the buttons are controlled using <code>on</code> expressions.  One drawback of using widgets inside other widgets is that they are not guaranteed to be loaded when <code>compileWidget</code> is called.  Therefore, you cannot reliably add event listeners to widgets in <code>compileWidget</code> and you should use on expressions instead.  In order to call functions within your module, you need to give the fully-qualified class name and surround the call in a script tag.</p>
<p>From this function, you need to return an object that looks like this:</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="javascript"><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #3366CC;">'presentation'</span> : html.<span style="color: #006600;">join</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #66cc66;">&#41;</span>,
	<span style="color: #3366CC;">'position'</span> : Appcelerator.<span style="color: #006600;">Compiler</span>.<span style="color: #006600;">POSITION_REPLACE</span>,
	<span style="color: #3366CC;">'compile'</span> : <span style="color: #003366; font-weight: bold;">true</span>,
	<span style="color: #3366CC;">'wire'</span> : <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #66cc66;">&#125;</span>;</pre></td></tr></table></div>

<p>What all this means:</p>
<table class="properties">
<tr>
<th>name</th>
<th>description</th>
<th>required</th>
</tr>
<tr>
<td>presentation</td>
<td>the html that will be inserted into the DOM</td>
<td>false</td>
</tr>
<tr>
<td>position</td>
<td>where whatever gets inserted goes</td>
<td>true</td>
</tr>
<tr>
<td>compile</td>
<td>set to true to have your <code>compileWidget</code> function be called</td>
<td>false</td>
</tr>
<tr>
<td>wire</td>
<td>set to true if you use Appcelerator (widgets, <code>on</code> attribute, etc) inside the widget</td>
<td>false</td>
</tr>
</table>
<p>Widgets aren&#8217;t required to have any presentation and could be completely non-visual.  Therefore, the two most common values for position are <code>Appcelerator.Compiler.POSITION_REMOVE</code>, which removes this element from the DOM entirely (for non-visual widgets) and <code>Appcelerator.Compiler.POSITION_REPLACE</code> which removes the Appcelerator markup and substitutes what was defined in <code>presentation</code>.  </p>
<p><strong>compileWidget: function()</strong><br />
The stopwatch needs to store the state of its clocks and timer somewhere and the logical place is the class-level hash-map called stopwatches.  In <code>compileWidget</code>, we create the entry for this particular stopwatch in the map:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript">compileWidget: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>params<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> id = params<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'id'</span><span style="color: #66cc66;">&#93;</span>;
    Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Stopwatch</span>.<span style="color: #006600;">stopwatches</span>.<span style="color: #006600;">set</span><span style="color: #66cc66;">&#40;</span>id, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'date'</span>: <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #3366CC;">'timer_id'</span>: <span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>,</pre></td></tr></table></div>

<p>You could also assign event listeners to any html elements here as well, if you choose to use that approach instead of <code>on</code> events.  </p>
<p><strong>getActions: function()</strong><br />
This returns a list of functions that can serve as actions when the widget receives a message.  The stopwatch allows you to start_stop, and clear_time.  You might pause here and think that &#8220;toggle&#8221; and &#8220;reset&#8221; might be more logical names, but these are global actions available on all elements.  Global action declarations take precedence over custom actions.</p>
<p>The code for this function is just:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript">getActions: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'start_stop'</span>, <span style="color: #3366CC;">'clear_time'</span><span style="color: #66cc66;">&#93;</span>;  
<span style="color: #66cc66;">&#125;</span>,</pre></td></tr></table></div>

<p><strong>Custom Actions</strong><br />
The get actions method allows you to define the custom actions that can be performed on the widget.  To implement a custom action you need to implement a function with a particular signature:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">start_stop: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>id,parameters,data,scope,version<span style="color: #66cc66;">&#41;</span></pre></div></div>

<table class="properties">
<tr>
<th>name</th>
<th>description</th>
</tr>
<tr>
<td>id</td>
<td>the id of your widget</td>
</tr>
<tr>
<td>parameters</td>
<td>included parameters</td>
</tr>
<tr>
<td>data</td>
<td>the data payload of the message</td>
</tr>
<tr>
<td>scope</td>
<td>the scope of the message, usually &#8216;appcelerator&#8217;</td</tr>
<tr>
<td>version</td>
<td>not used</td>
</tr>
</table>
<p>The full implementation of the <code>start_stop</code> function is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript">start_stop: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>id,parameters,data,scope,version<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> stopwatch = Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Stopwatch</span>.<span style="color: #006600;">stopwatches</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>stopwatch.<span style="color: #006600;">timer_id</span> <span style="color: #66cc66;">!</span>= <span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Stopwatch</span>.<span style="color: #000066;">stop</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span> 
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #66cc66;">&#123;</span>
        Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Stopwatch</span>.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>,</pre></td></tr></table></div>

<p><strong>Responding to messages and events</strong><br />
There are a few ways you can do this, which you choose depends on your needs.  The standard is to not do anything.  Your widget will automatically respect the <code>on</code> attributed specified on it. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #66cc66;">&lt;</span>app:script on=<span style="color: #3366CC;">&quot;l:toggle_me then start_stop&quot;</span><span style="color: #66cc66;">&gt;&lt;/</span>app:script<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>will call the function <code>start_stop</code> on the message <code>l:toggle_me</code>.  No additional code was required.</p>
<p>However, this doesn&#8217;t always work, such as when an element internal to the widget needs to respect the <code>on</code> and not the widget itself.  In this case, you need to over-ride the function <code>dontParseOnAttributes</code> like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript">dontParseOnAttributes: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span>,</pre></td></tr></table></div>

<p>then manually add the contents of <code>on</code> to the required element in your <code>buildWidget</code> function.  Also, remember to set <code>wire</code> to <code>true</code>.</p>
<p><strong>Registering Module Resources</strong><br />
Every module has at least one resource that it will need to register (itself).  To do this, you need to call</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Appcelerator.<span style="color: #006600;">Compiler</span>.<span style="color: #006600;">registerModule</span><span style="color: #66cc66;">&#40;</span>moduleName, moduleClass<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>outside of the class declaration for your module.<br />
If you have third party javascript, you need to call:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Appcelerator.<span style="color: #006600;">Core</span>.<span style="color: #006600;">registerModuleWithJS</span><span style="color: #66cc66;">&#40;</span>moduleName,moduleClass,<span style="color: #66cc66;">&#91;</span>list_of_files<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>instead.  The javascript must be in the javascript directory under the module root.</p>
<p>If your module has any module-specific css, you will also need to call </p>
<pre lang=javascript">
Appcelerator.Compiler.loadModuleCSS(moduleName,cssFileName).</pre>
<p>For this call to work, the file must be in the module’s css directory.  </p>
<p>Since the stopwatch has css, but no javascript, you&#8217;ll find the following at the very bottom of <code>app_stopwatch.js</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript">Appcelerator.<span style="color: #006600;">Core</span>.<span style="color: #006600;">registerModule</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'app:stopwatch'</span>, Appcelerator.<span style="color: #006600;">Module</span>.<span style="color: #006600;">Stopwatch</span><span style="color: #66cc66;">&#41;</span>;
Appcelerator.<span style="color: #006600;">Core</span>.<span style="color: #006600;">loadModuleCSS</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'app:stopwatch'</span>, <span style="color: #3366CC;">'stopwatch.css'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>The Final Product</strong><br />
The stopwatch is now complete.  You can see the full source code at https://svn.appcelerator.org/appcelerator_sdk/trunk/src/web/modules/app_stopwatch/.</p>
<p>The stopwatch widget is slated to be included in the Appcelerator 2.1 release.  Once that release goes live, so to will the <a href="http://unittest.appcelerator.org/index.html">Web Units</a>.</p>
<p>Any questions about this guide or building widgets should be directed towards our google group, appcelerator-platform-sdk@googlegroups.com.</p>
<p><a href="http://sharethis.com/item?&wp=2.8.4&amp;publisher=fb25d17b-38b5-47ed-9857-f171051e9899&amp;title=Building+Appcelerator+Widgets&amp;url=http%3A%2F%2Fwww.appcelerant.com%2Fbuilding-appcelerator-widgets.html">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.appcelerant.com/building-appcelerator-widgets.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
