<?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>Transcending Frontiers &#187; Extending the Dojox Datagrid with Formatters</title>
	<atom:link href="http://blog.thefrontiergroup.com.au/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thefrontiergroup.com.au</link>
	<description>Your peek inside the collective mind of The Frontier Group</description>
	<lastBuildDate>Tue, 03 Jan 2012 06:53:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Extending the Dojox Datagrid with Formatters</title>
		<link>http://blog.thefrontiergroup.com.au/2009/10/extending-the-dojox-datagrid-with-formatters/</link>
		<comments>http://blog.thefrontiergroup.com.au/2009/10/extending-the-dojox-datagrid-with-formatters/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 04:09:00 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Inside TFG]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=455</guid>
		<description><![CDATA[I&#8217;m currently trying to create a Dojo widget that extends dojox.grid.DataGrid and has sub grids inside it. It&#8217;ll likely expand over time to be somewhat of a complex beast so there wasn&#8217;t really any other way than going the self contained widget. Having a bunch of global functions and DOM manipulation would be disgusting. I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently trying to create a Dojo widget that extends dojox.grid.DataGrid and has sub grids inside it. It&#8217;ll likely expand over time to be somewhat of a complex beast so there wasn&#8217;t really any other way than going the self contained widget. Having a bunch of global functions and DOM manipulation would be disgusting. </p>
<p>I&#8217;m building this widget in an incremental way so after getting the layout and hooking it up to a data source I moved on to creating a column with an expand/contract link that would trigger the sub grid to expand and contract as required. I ran into a problem when trying to keep my formatting functions contained within the widget definition. </p>
<p>I setup my layout in the following way :</p>
<pre class="javascript" name="code">
 _mainGridLayout : [
      { type : "dojox.grid._RowSelector" },
      {
        cells : [[
          { name : '', get : this._getBlank, formatter : this._formatExpander },
          { field : "customer_id", name: "Cust ID" },
          { field : "customer_name", name : "Customer" },
          { field : "job_number", name : "Job No." },
            ...
        ]]
      }
    ],
</pre>
<p>I had defined the get function and formatter function in my widget, but whenever I loaded the widget I&#8217;d get a blank column as if neither function had been run. </p>
<p>After some poking and prodding it seemed that whatever context this view was being built in, the &#8216;this&#8217; object was not my widget as I originally expected. The &#8216;this&#8217; object was actually an instance of dojox.grid.cell. That was okay as a cell has a reference to it&#8217;s grid, so what I needed to do was access <code>this.grid._formatExpander</code> and <code>this.grid._getBlank</code>. </p>
<p>I did it in the following way : </p>
<pre class="javascript" name="code">
          { name : '', get : function() {return this.grid._getBlank(arguments);}, formatter : function() {return this.grid._formatExpander(arguments);} },
</pre>
<p>It worked perfectly, I&#8217;d like to say just as I expected, but alas I&#8217;m not that smart :)</p>
<p>It feels to me like there should be a cleaner way to do it, but at least for the time being it works fine and doesn&#8217;t required me to move my formatting functions out into some global namespace and outside of my widget&#8217;s nice pieces of logic. </p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2009/10/extending-the-dojox-datagrid-with-formatters/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2009/10/extending-the-dojox-datagrid-with-formatters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keeping Context in Your AJAX Callbacks</title>
		<link>http://blog.thefrontiergroup.com.au/2009/03/keeping-context-in-your-ajax-callbacks/</link>
		<comments>http://blog.thefrontiergroup.com.au/2009/03/keeping-context-in-your-ajax-callbacks/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 08:19:04 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=356</guid>
		<description><![CDATA[These days I&#8217;m using a lot of asynchronous calls to get data and dynamically build the UI on the client side. It generally allows a far nicer experience to be provided to the user, being able to update parts of the UI without reloading the whole page is one of the first steps to your [...]]]></description>
			<content:encoded><![CDATA[<p>These days I&#8217;m using a lot of asynchronous calls to get data and dynamically build the UI on the client side. It generally allows a far nicer experience to be provided to the user, being able to update parts of the UI without reloading the whole page is one of the first steps to your apps being able to wear a Web 2.0 moniker. </p>
<p>The general pattern for me these days has become : </p>
<pre class="javascript" name="code">
var callback = function CallBack(data) {
    ... Do Some Processing ....
}

var input_data = GatherData();
MakeRequest(target_url, data, callback);
</pre>
<p>I tend to use jQuery and so my callback is passed in the returned data from the target_url. My call back function then generally performs some tasks on the UI based on what it receives.</p>
<p>The problem though is that in this pattern you can&#8217;t get any data from the context when <code>MakeRequest()</code> is called into the scope of <code>CallBack</code>. It&#8217;s a scoping issue that falls outside of this little post, but if you&#8217;d like an explanation of how Javascript handles scope of variables then you can Google for Javascript scope chain or take a shortcut to <a href="http://www.digital-web.com/articles/scope_in_javascript/">this article</a>. Essentially when the call is made to <code>CallBack()</code> all it will have is it&#8217;s own variables and any globally accessible (window) variables. </p>
<p>This week though I had a thought and worked through it with one of my work mates, Tony. If you passed in a function that had the scope that included the context you wanted to pass, then maybe you&#8217;d be able to access whatever data from the callee you wanted. It turns out that this does work. </p>
<p>The way to do this is pretty simple, and I used it to create a simple function that would grab data and then populate a select element with options. In this case the context I&#8217;d like to keep is which select element is the target. </p>
<p>Say that I needed to run this over a bunch of select elements in quick succession then as the callbacks were issued they may end up out of the initial execution order, so the target element isn&#8217;t reliable if it&#8217;s been stored in a global variable. I could pass it in as a variable that would come back from the page that is returning the data but that just <a href="http://en.wikipedia.org/wiki/Code_smell">smells bad</a> to me. I think potentially JSONP is an alternative too, but this felt like the right way. </p>
<pre class="javascript" name="code">
$.getJSON(url, input_data,
	(function(target_element) {
		return function(response_data) {
			var html = [];

			for (var i = 0; i < response_data.length; i++) {
				item = response_data[i];

				html.push('
<option value="' + item['key'] + '">' + item['value'] + '</option>

');
			}

			target_element.children(':gt(0)').remove();
			target_element.append(html.join(''));
		};
	})(target_element)
);
</pre>
<p>Essentially the main thing that has changed is that we are now running an anonymous function at the time that the AJAX call is issued. This anonymous function itself returns a function that matches the signature that jQuery is expecting for the callback function. The scope in which this function runs contains the target_element because it was passed into the anonymous function as a parameter. I&#8217;m tempted to say that it&#8217;s all crazy Javascript scoping, but in reality it&#8217;s very cool and very powerful. </p>
<p>If you want to see the execution order of this then just put a some logging into the anonymous function and the callback function and you&#8217;ll see what I mean. It will probably make it easier to see what is going on too.</p>
<p>I&#8217;ve run into issues trying to get around these problems before and thankfully as mine and the team&#8217;s knowledge of Javascript increases I&#8217;m finding better and better solutions. I thought my previous method of approaching this problem was quite hacky but now I don&#8217;t feel so dirty. </p>
<p>Thanks to Tony too for working through this with me! </p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2009/03/keeping-context-in-your-ajax-callbacks/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2009/03/keeping-context-in-your-ajax-callbacks/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Advanced Javascript with Douglas Crockford</title>
		<link>http://blog.thefrontiergroup.com.au/2009/02/advanced-javascript-with-douglas-crockford/</link>
		<comments>http://blog.thefrontiergroup.com.au/2009/02/advanced-javascript-with-douglas-crockford/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 23:29:17 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Presentation]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=330</guid>
		<description><![CDATA[I&#8217;ve been slowly making my way through Douglas Crockford&#8217;s talk on Advanced Javascript and even at only about 3/4 of the way through the first installment I&#8217;ve learned plenty! The presentation is in three parts and so far he&#8217;s covered a bunch of standard programming patterns and elements (singletons, private members, namespaces, constructors, etc&#8230;) implemented [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been slowly making my way through <a href="http://en.wikipedia.org/wiki/Douglas_Crockford">Douglas Crockford&#8217;s</a> talk on <a href="http://video.yahoo.com/watch/111585/1027823">Advanced Javascript</a> and even at only about 3/4 of the way through the first installment I&#8217;ve learned plenty!</p>
<p>The presentation is in three parts and so far he&#8217;s covered a bunch of standard programming patterns and elements (singletons, private members, namespaces, constructors, etc&#8230;) implemented using Javascript as well as a detailed account of how the language is implemented. Rather than having to work out by trial and error what variables and functions are available where, or how to implement private functions or a singleton, he&#8217;s given you the first principles so you can work it out for yourself and a bunch of useful examples.</p>
<p>I highly recommend watching it. I find the more languages I learn the better I get at doing things across the board. </p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2009/02/advanced-javascript-with-douglas-crockford/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2009/02/advanced-javascript-with-douglas-crockford/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>25 Tips for Better jQuery</title>
		<link>http://blog.thefrontiergroup.com.au/2008/12/25-tips-for-better-jquery/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/12/25-tips-for-better-jquery/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 01:39:20 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Websites or Tools]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=254</guid>
		<description><![CDATA[Typically these types of &#8220;25 Tips To&#8230;&#8221; or &#8220;10 Things That&#8230;&#8221; style articles have a few good points but are filled with fluff. However the article 25 Tips for Better jQuery is full of great points that cover architecture, coding conventions, compatibility and performance. If you are looking to get a better handle on jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>Typically these types of &#8220;25 Tips To&#8230;&#8221; or &#8220;10 Things That&#8230;&#8221; style articles have a few good points but are filled with fluff. However the article <a href="http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx">25 Tips for Better jQuery</a> is full of great points that cover architecture, coding conventions, compatibility and performance.</p>
<p>If you are looking to get a better handle on jQuery and more importantly how to use it well, I haven&#8217;t seen many better places to start.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/12/25-tips-for-better-jquery/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/12/25-tips-for-better-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling Permissions for AJAX Requests</title>
		<link>http://blog.thefrontiergroup.com.au/2008/12/handling-permissions-for-ajax-requests/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/12/handling-permissions-for-ajax-requests/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 11:10:12 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=248</guid>
		<description><![CDATA[Again I have a website where a lot of data transfer is done asynchronously and a large amount of the presentation is done using Javascript. Different users have different access to features across the site, and I can&#8217;t just rely on hiding links given the data is a simple HTTP request away. Protecting this data [...]]]></description>
			<content:encoded><![CDATA[<p>Again I have a website where a lot of data transfer is done asynchronously and a large amount of the presentation is done using Javascript. Different users have different access to features across the site, and I can&#8217;t just rely on hiding links given the data is a simple HTTP request away. Protecting this data on the server side has always been easy to me, but I&#8217;ve typically found building the persistent abstractions I like to have far more difficult on the client side. As per usual, it&#8217;s probably just another issue I haven&#8217;t spent enough time to get a grip on. </p>
<p>It&#8217;s possible technologies such as Prism and Gears will help with this in the future. Unfortunately, it is the present.</p>
<p>This time I think I have a solution that I&#8217;m pretty happy with though. On the server side it involves using the existing HTTP response codes to indicate to the requester what happened with their request. On the client side the <code>ajaxComplete()</code> event is used to handle these codes. </p>
<p>jQuery will automatically call the function you specify as your callback in an AJAX request if the request is successful, so I&#8217;m only interested in handling failures. At the moment I&#8217;m assuming that all of my calls use JSON for their data format, but the alternative is a case I can handle later if need be. Only do what is necessary right now is a great credo I think.</p>
<p>So here is my event handler, it&#8217;s very simple but the documentation on the arguments to the event are a little slight. The <code>success()</code> call just makes a call to the function specified in the original call, hence passing in an empty array, simulating no records returned. The code I&#8217;m handling is for 401: Unauthorized, which in this case is the truth. This code will be sent back when I determine that the user is trying to access some data they aren&#8217;t supposed to. <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP codes</a> handle the majority of cases you&#8217;ll run into.</p>
<pre name="code" class="javascript">
(function() {
	if (typeof(jQuery) != 'undefined') {
		jQuery().ajaxComplete(function(ev, req, settings) {
			if (req.status == 401) {
				settings.success([]);
				alert('You have insufficient privileges.');
			}
		});
	}
})();
</pre>
<p>The server side code is simple, it&#8217;s just a matter of sending back the appropriate <a href="http://php.net/header">header</a>:</p>
<pre name="code" class="php">
	header('HTTP/1.0 401 Insufficient Privileges', false, 401);
</pre>
<p>This function is specified in a global include where part of the website uses prototype, I&#8217;ve been slowly integrating jQuery. Therefore the first thing I do is check if jQuery has been defined, if it has then I register my function as the handler for the <code>ajaxComplete</code> event. Since it&#8217;s declared globally this will happen on every AJAX call. If the response code is 401 then first I pass back and empty array to my <code>success</code> handler so that the little loading notifier disappears, and then I notify the user of the error. </p>
<p>It seems to be a trend lately, but again this is just a very simple idea but I hope it saves someone some hassle. I know I&#8217;ve searched high and low on the topic and haven&#8217;t found a nice generic solution. </p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/12/handling-permissions-for-ajax-requests/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/12/handling-permissions-for-ajax-requests/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Animating Table Rows with jQuery</title>
		<link>http://blog.thefrontiergroup.com.au/2008/12/animating-table-rows-with-jquery/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/12/animating-table-rows-with-jquery/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 13:55:19 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[AuroraCMS]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Product Reviews]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=220</guid>
		<description><![CDATA[Animating table rows in the browser is problematic. You see, they aren&#8217;t block elements and as such don&#8217;t have a height or width property. Instead they take their constraints from the content inside them, and the elements that contain them. For rows this typically means they&#8217;re constrained by the containing table, and filled by the [...]]]></description>
			<content:encoded><![CDATA[<p>Animating table rows in the browser is problematic. You see, they aren&#8217;t block elements and as such don&#8217;t have a height or width property. Instead they take their constraints from the content inside them, and the elements that contain them. For rows this typically means they&#8217;re constrained by the containing table, and filled by the contained columns.</p>
<p>Today I wanted to slide a row up, and then when it had finished sliding I wanted to remove it from the DOM. Essentially giving it a nice effect when something is deleted.</p>
<p>Given that the height of a row is controlled by it&#8217;s content, I figured the easiest way to do this would be to wrap all of the content inside each column with a block element, in this case a <code>div</code>, and then resize those.</p>
<p>jQuery makes this extremely easy :</p>
<pre name="code" class="javascript">var el = $(options.element_prefix + id);
el.children("td").each(function() {
    $(this).wrapInner("< div />").children("div").slideUp(function() {el.remove();})
});</pre>
<p>NOTE : The div tag in the <code>wrapInner()</code> is malformed because it won&#8217;t display properly otherwise. Please remove the space between the opening bracket and &#8216;div&#8217;. </p>
<p>It&#8217;s all pretty easy to understand. Essentially my root element is a row, and so for each <code>td</code> in that row wrap it&#8217;s content in a <code>div</code>. Then for the child <code>divs</code> in each <code>td</code>, run the <code>slideUp()</code> method. The callback in the <code>slideUp()</code> method says after the animation is done, remove the row. Given the speed of computers these days, no one will notice that the last few columns quite likely just vanish instead of complete their animation.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/12/animating-table-rows-with-jquery/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/12/animating-table-rows-with-jquery/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery: Check For Non-Empty Input Fields</title>
		<link>http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 08:46:07 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=176</guid>
		<description><![CDATA[&#8211; Check out some of our more recent Ruby on Rails blog posts. If you&#8217;d like to hire our team, get in touch &#8211; I was writing some code today and I wanted to prompt the user to check if they wanted to delete a row of data only if there was some data they [...]]]></description>
			<content:encoded><![CDATA[<p><em>&#8211; Check out some of our more recent </em><a title="Ruby on Rails posts" href="http://thefrontiergroup.com.au/blog/category/ruby-on-rails/"><em>Ruby on Rails blog posts</em></a><em>. If you&#8217;d like to hire our team, <a title="Get in touch" href="http://thefrontiergroup.com.au/pages/contact-us">get in touch</a></em><em> &#8211;</em></p>
<p>I was writing some code today and I wanted to prompt the user to check if they wanted to delete a row of data only if there was some data they might not want deleted. Typically to do this I would loop through all the input fields and if any of them weren&#8217;t blank I would run the check. I figured that there must be a better way with callbacks or selectors, and after a little thinking I wrote this jQuery snippet :</p>
<pre class="javascript">row.find('input[value!=""]').length &gt; 0</pre>
<p>This basically says count the number of inputs with a non-blank value you find, inside the given row.</p>
<p>Just like <a href="http://thefrontiergroup.com.au/blog/2008/11/dynamically-reducing-object-sizes-in-php/">my last post</a>, it&#8217;s nothing amazing but it sure is a better way of doing things. If you look into the <code>andSelf()</code> method then you could also easily chain selects and other input types.</p>
<p>I&#8217;m also interested if someone has a better way to approach this problem?</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/11/jquery-check-for-non-empty-input-fields/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JavaScript and String Building</title>
		<link>http://blog.thefrontiergroup.com.au/2008/11/javascript-and-string-building/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/11/javascript-and-string-building/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 01:53:56 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=127</guid>
		<description><![CDATA[In most languages there is a means of building strings and printing out messages. Many will find the printf command all too familiar. In the JavaScript space however things have been a little slow to develop. We have our strings and to build them up we merely start adding them together but as our code [...]]]></description>
			<content:encoded><![CDATA[<p>In most languages there is a means of building strings and printing out messages. Many will find the printf command all too familiar. In the JavaScript space however things have been a little slow to develop.</p>
<p>We have our strings and to build them up we merely start adding them together but as our code gets more elaborate this gets harder and harder. The biggest problem is usually trying to figure out how to escape quotes and inverted commas as we swap back and forth between plain strings and JavaScript variables.</p>
<p>I&#8217;ve often thought there must be a better way and working with the Dojo toolkit there is. It can be found in the method &#8220;dojo.string.substitute&#8221;. This mirrors the idea used in other languages of passing the method a template string and an object of values to substitute in. Whilst Dojo allows for numerous ways of passing the data such as arrays and functions the one that interests me the most is a standard JavaScript object which is effectively a hash map of name and value pairs.</p>
<p>Here&#8217;s a simplified version of the Dojo function that only uses objects for the value map.</p>
<pre class="js" name="code">var tfg = {
  string:{
    substitute: function(/*String*/ template, /*Object*/ valueMap){
      return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match, key){
            return (valueMap[key])?valueMap[key]:"";
        });
    }
  }
}</pre>
<p>The old way of doing things might look like this:</p>
<pre class="js" name="code">var data = {name:"bob", occupation: "builder"};
var string = "His name is "+data.name+" and is occupation is "+data.occupation+".";</pre>
<p>Here we merely add the different parts of the string and string values together. Simple enough in a string this size but as it grows and we start working with HTML syntax things get more complicated.</p>
<p>Using the substitute way:</p>
<pre class="js" name="code">var data = {name:"bob", occupation: "builder"};
var template = "His name is ${name} and is occupation is ${occupation}.";
var str = tfg.string.substitute(template, data);</pre>
<p>Here anything surrounded by the format &#8220;${something}&#8221; is replaced with the value found in &#8220;data.something&#8221;.</p>
<p>A major advantage of this approach is when we start looking at fetching JSON data sets from a remote server via Ajax. We almost always have to process the return values into something to display and if it is looping through a list of objects we can now define the template once and repeatedly create a string with our substitute method. Here&#8217;s a simple idea using JQuery for the loop.</p>
<pre class="js" name="code">var result = [{name:"a",company:"1"},{name:"b",company:"2"}];
var template = "&lt;tr&gt;&lt;td&gt;${name}&lt;/td&gt;&lt;td&gt;${company}&lt;/td&gt;&lt;/tr&gt;";
var rows = $.map(result, function(index, item){
  return tfg.string.substitute(template, item);
})
var tableBodyString = rows.join("\n");</pre>
<p>Here we looped through the items substituting each new set of values and pushing the result into an array. At the end we merge the array into a single string with &#8220;join&#8221; and have the body of a HTML table ready to be used in the page.</p>
<p>This approach is fast, easier to read and much cleaner to implement when looping through data.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/11/javascript-and-string-building/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/11/javascript-and-string-building/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MochiKit 1.4 is Released</title>
		<link>http://blog.thefrontiergroup.com.au/2008/10/mochikit_14_release/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/10/mochikit_14_release/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 01:48:49 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Websites or Tools]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[John Resig]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MochiKit]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=107</guid>
		<description><![CDATA[I&#8217;m a big fan of Javascript frameworks that help me get things done, I&#8217;m a huge fan of anything that helps me get things done easier. My favourite framework though is jQuery after having tried a few just over a year ago I settled on jQuery as a better choice than Prototype and thankfully I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of Javascript frameworks that help me get things done, I&#8217;m a huge fan of anything that helps me get things done easier. My favourite framework though is <a href="http://jquery.com">jQuery</a> after having tried a few just over a year ago I settled on jQuery as a better choice than Prototype and thankfully I&#8217;ve been proven right.</p>
<p>John Resig, the creator of jQuery is also a bit of a proponent for another framework called <a href="http://mochikit.com">MochiKit</a>. The idea of MochiKit is to &#8220;Make Javascript Suck Less&#8221; and as such doesn&#8217;t really offer many out there features that others might, it more concentrates on filling in functionality gaps that are present in the Javascript language.</p>
<p>These things include better comparator functions, which enable sorting and so on, also there are handy formatting functions, asynchronous functions and data functions for handling JSON. DOM manipulation looks to be very good, though whether it&#8217;s easier than jQuery is probably up to personal choice. Basically MochiKit seems to fill in the gaps of Javascript and do exactly what it says it does, make programming with Javascript suck less by giving you the functions you always wished you had.</p>
<p>I plan on checking this out and feel like it&#8217;s probably a worth addition to anybodys&#8217; toolbox that is writing client side code.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/10/mochikit_14_release/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/10/mochikit_14_release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced Javascript</title>
		<link>http://blog.thefrontiergroup.com.au/2008/10/advanced-javascript/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/10/advanced-javascript/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 01:18:25 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Industry Trends]]></category>
		<category><![CDATA[Websites or Tools]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[John Resig]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=91</guid>
		<description><![CDATA[When I started writing web applications I spent the vast majority of my time writing server side presentation code. As I progressed and people found out I knew a little bit about databases I shifted towards spending most of my time between designing and implementing data code and server side presentation code. Now I would [...]]]></description>
			<content:encoded><![CDATA[<p>When I started writing web applications I spent the vast majority of my time writing server side presentation code. As I progressed and people found out I knew a little bit about databases I shifted towards spending most of my time between designing and implementing data code and server side presentation code.</p>
<p>Now I would say I spend the majority of my time between writing server and client side presentation code, in other words I write a lot more time writing Javascript now than I ever have before and things don&#8217;t look like changing.</p>
<p>The majority of my work is with <a href="http://jquery.com">jQuery</a>, but we have projects deployed using standard <a href="http://en.wikipedia.org/wiki/JavaScript">Javascript</a> and <a href="http://www.prototypejs.org/">Prototype</a> as well so it pays to have as good an understanding of Javascript as possible. With that in mind I found a <a href="http://ejohn.org/apps/learn/">great site</a> aimed at teaching some advanced uses of the language. It was written by <a href="http://ejohn.org/">John Resig</a> who is one of the real leaders in using Javascript in interesting ways, he&#8217;s well known for developing the jQuery library but also has his finger in other pies most notable of which is probalby <a href="http://getfirebug.com/">Firebug</a>.</p>
<p>I remember when I started writing web applications that Javascript was almost laughed at, but now I absolutely enjoy writing it and is a massively important player on the web. I think people stopped expecting the user interaction of a web application to naturally lack that of a desktop application a little while ago and Javascript and frameworks like jQuery and Prototype allowed that to happen.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/10/advanced-javascript/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/10/advanced-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

