<?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>Michael Forbes - User Experience Engineer &#187; jquery</title>
	<atom:link href="http://mrforbes.com/blog/category/javascript/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://mrforbes.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 20 Feb 2012 04:23:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Thinking Big&#8230; Architecting a large application with jQuery / Backbone / Require, an Overview</title>
		<link>http://mrforbes.com/blog/2012/02/thinking-big-architecting-a-large-application-with-jquery-backbone-require-an-overview/</link>
		<comments>http://mrforbes.com/blog/2012/02/thinking-big-architecting-a-large-application-with-jquery-backbone-require-an-overview/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 04:18:47 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[backbone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://mrforbes.com/blog/?p=235</guid>
		<description><![CDATA[A Little Background Recently at work we&#8217;ve been in heavy development mode on a new thick-client application architecture.  If you aren&#8217;t familiar with the concept, thick-client essentially means putting most of the work onto the client&#8217;s processor, and removing it &#8230; <a href="http://mrforbes.com/blog/2012/02/thinking-big-architecting-a-large-application-with-jquery-backbone-require-an-overview/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>A Little Background</strong></p>
<p>Recently at work we&#8217;ve been in heavy development mode on a new thick-client application architecture.  If you aren&#8217;t familiar with the concept, thick-client essentially means putting most of the work onto the client&#8217;s processor, and removing it from the server.  The benefits of this are many, not the least of which is a much faster site response time for your users, and a much lighter load for your server.</p>
<p>To accomplish this basically means using javascript and a lot of AJAX, as well as implementing the application such that that a) search engines can still index your site and b) the user can both bookmark and use the back button wherever they are in your app.</p>
<p>In order to facilitate this heavy use of ajax and management of state, a number of MVC(ish) patterned javascript libraries have been created, and more appear seemingly every week.  After a little bit of research, we settled on Backbone due to its barebones nature, low overhead, lesser learning curve, and the size of the community.  It always helps to be able to reach out when you get stuck.</p>
<p>In combination with jQuery (we are already using it heavily), and Require.js (a dynamic resource loader which has also been in use for quite a while), we had the perfect trinity of tools to get the job done.</p>
<p><strong>Basic App Structure</strong></p>
<p>There are a lot of things to consider when architecting a large application.  For HMS, this is compounded by the fact that a) we have a lot of users with a lot of different site configurations, b) we have different variations on the main backend application, c) there is a lot of legacy code and newer front-end logic that needs to maintain compatibility so that pieces can be added one at a time.   This is where Require and its support of AMD (asynchronous module definition) really shines.</p>
<p>All of our thick-client application code is managed through AMD, and there are two things about it that are very awesome:</p>
<ol>
<li>It allows seamless integration on a module-by-module basis with the current application code</li>
<li>Even portions of modules can be broken out and used before their parent module is complete &#8211; for example, we already have the autocomplete module live, even though the search module is still in development. When the time comes, it will be trivial to move it back into its proper place.</li>
</ol>
<p>The general application structure goes like this:</p>
<ol>
<li>Event Aggregator<br />
<a href="http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/" target="_blank">Outlined here</a>, we have a single global object, and its job is to manage communication between modules.</li>
<li>Outer Router<br />
We have two routers.  This helps keep the application router a lot simpler, and keeps module related logic inside the module. The outer router manages routes for the entire application, and determines which modules to load.</li>
<li>User Module<br />
Right now, this is the only module that sits everywhere in the application. As such, it is loaded by the outer router.</li>
<li>Modules<br />
Each module contains its own router as well as templates and views. The router loads the views, the views load the templates (we&#8217;re using Hogan).  When we compile with require, each module (including templates) becomes a single file (the module router).</li>
<li>Models<br />
The models are the glue between the client and the server,  and they sit in their own folder because multiple modules may use the same models.</li>
<li>Form Mediator<br />
This is actually one of the modules, but its worth describing here.  We use a special Backbone view to handle every form, enhancing functionality as we go.  This view simplifies forms immensely, managing the model, validation, and state with ease.  Its special power is to allow multiple modules to combine into a single form definition &#8211; most usefully for search / advanced search.</li>
<li>UI Modules<br />
Most DOM manipulation (outside of insertions/removals)  and all UI effects are offloaded into separate UI modules.  The reasons for this are a) it separates presentation concerns and b) it allows for a much easier time should we decide to switch from jQuery for future manipulation (one can dream of full standards support across browsers)</li>
<li>Backbone.Store (localStorage)<br />
Of course we&#8217;re using this for our app.  Currently it persists forms, helps maintain the user session client-side, and caches model data.</li>
</ol>
<p>Obviously, there are many ways to architect a thick-client application.  Our approach intends to prevent module dependency (outside of UI Modules), separate DOM manipulation from data logic, keep communication centralized in an event aggregator, load quickly (through smart module loading and use of localStorage, among other things), and preserve the global namespace through the use of AMD.  There are a lot of details I&#8217;ve left out (for instance reuse for mobile), but I do hope to dive a little deeper in future posts.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2012/02/thinking-big-architecting-a-large-application-with-jquery-backbone-require-an-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing Backbone.Store &#8230; JSON storage for Backbone.</title>
		<link>http://mrforbes.com/blog/2012/01/introducing-backbone-store-json-storage-for-backbone/</link>
		<comments>http://mrforbes.com/blog/2012/01/introducing-backbone-store-json-storage-for-backbone/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 03:16:21 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[backbone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://mrforbes.com/blog/?p=224</guid>
		<description><![CDATA[Right now, you can&#8217;t do much better for quickly developing javascript applications and modules than the triumvirate of  Backbone, jQuery, and Require.  Each of these libraries fit together so well (IMO), and have increased my personal productivity immensely &#8211; improving quality, performance, &#8230; <a href="http://mrforbes.com/blog/2012/01/introducing-backbone-store-json-storage-for-backbone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Right now, you can&#8217;t do much better for quickly developing javascript applications and modules than the triumvirate of  Backbone, jQuery, and Require.  Each of these libraries fit together so well (IMO), and have increased my personal productivity immensely &#8211; improving quality, performance, readability, and consistency.</p>
<p>There was already a great library for Backbone to utilize HTML5 localStorage functionality; however, there were a few drawbacks to the library that caused me to look in another direction, and ultimately led me to creating Backbone.store.</p>
<p>My two greatest needs were:</p>
<ol>
<li>The ability to utilize the storage mechanism as a cache.</li>
<li>A greater range of compatibility outside of localStorage support.</li>
</ol>
<p>To meet these needs, I turned to <a title="Lawnchair" href="http://westcoastlogic.com/lawnchair/" target="_blank">Lawnchair</a>, a JSON storage library with a wide range of adapters for different storage types, mixed in a little bit of the aforementioned backbone.localstorage plugin, and came out with the beginnings of a (hopefully) useful storage middleware.</p>
<p>While the overall project is in very early stages, its not too soon to look at, play with, and provide feedback on.</p>
<p><a title="Backbone.store" href="https://github.com/mrforbes/Backbone-Store" target="_blank">Download / clone from Github.</a></p>
<p><strong>Features:</strong></p>
<ol>
<li>Can be used with or without a backend server</li>
<li>Configurable cache time to live</li>
<li>Configure when to use server vs cached model &#8211; great for list  vs. detail of a record</li>
<li>Does not require localStorage support, though a Lawnchair adapter may be needed to get the support you want.</li>
</ol>
<p><strong>Planned enhancements:</strong></p>
<ol>
<li>Batch model upload to server (online/offline or time delayed)</li>
<li>Parameter based caching (for caching paging / search results / etc)</li>
<li>Remove Require (AMD) dependency</li>
<li>??? (accepting ideas)</li>
</ol>
<p>Also included in the project is a basic &#8216;Todo&#8217; style application including a CodeIgniter (php) based RESTful web server and structure, and a Backbone/Require/jQuery front-end consisting of a list/detail view with complete CRUD control and proper routing using both direct (bookmarkable) URL access and pushState routing.</p>
<p>Again, this project is nowhere near complete, so I&#8217;m sure there are some bugs&#8230; Still on the todo list is to add test cases using Sinon and Jasmine, and there is a known issue with the list -&gt; detail -&gt; list navigation.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2012/01/introducing-backbone-store-json-storage-for-backbone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery DynoMenu &#8211; a plugin with a purpose</title>
		<link>http://mrforbes.com/blog/2011/12/jquery-dynomenu-a-plugin-with-a-purpose/</link>
		<comments>http://mrforbes.com/blog/2011/12/jquery-dynomenu-a-plugin-with-a-purpose/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 01:57:21 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://mrforbes.com/blog/?p=221</guid>
		<description><![CDATA[I&#8217;ve just added the first of hopefully many jQuery plugins and bits of useful javascript to Github.  Called DynoMenu, it is a plugin with a very specific and singular purpose.. yet it may have value to others who find themselves &#8230; <a href="http://mrforbes.com/blog/2011/12/jquery-dynomenu-a-plugin-with-a-purpose/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just added the first of hopefully many jQuery plugins and bits of useful javascript to Github.  Called <a title="dynomenu" href="https://github.com/mrforbes/jQuery-DynoMenu">DynoMenu</a>, it is a plugin with a very specific and singular purpose.. yet it may have value to others who find themselves with a similar need.</p>
<p>DynoMenu was conceived to solve a problem on HomesConnect.  Since HomesConnect is a CMS that allows users to add new pages and new menu items to their navigation, and since the navigation is horizontal across the top of the page, the need arose to be able to maintain the layout of the templates while removing the burden of keep the navigation usable from the agent.</p>
<p>The solution was this plugin, which will first try to shrink the navigation font to a determined size, and if failing will systematically remove navigation items until it achieves the best fit. These items are placed in a &#8216;more&#8217; link which becomes the last main navigation item, and can be accessed from there.  Since the agent can determine the order of their navigation via a drag and drop interface on the backend, it is easy for them to decide which items will fall under the &#8216;more&#8217; link by pushing them to the end of the navigation.</p>
<p>In any case this may not have much use outside of CMS systems where you cannot control the total number of nav items, but who knows.. maybe there is a use case I&#8217;m not considering.</p>
<p><a href="https://github.com/mrforbes/jQuery-DynoMenu">Download it from Github here</a>.<br />
<a href="http://mrforbes.com/site/dynomenu">See a demo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2011/12/jquery-dynomenu-a-plugin-with-a-purpose/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fading PNGs in Internet Explorer 7 and 8</title>
		<link>http://mrforbes.com/blog/2009/10/fading-pngs-in-internet-explorer-7-and-8/</link>
		<comments>http://mrforbes.com/blog/2009/10/fading-pngs-in-internet-explorer-7-and-8/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 20:08:47 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Internet Explorer Hacks]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ie]]></category>

		<guid isPermaLink="false">http://www.mrforbes.com/?p=166</guid>
		<description><![CDATA[How to get rid of that annoying black background on PNGs that change opacity in IE8 <a href="http://mrforbes.com/blog/2009/10/fading-pngs-in-internet-explorer-7-and-8/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One issue I recently came across was the desire to use PNGs as the background of a tooltip, and to fade the tooltip in and out of view (you can see the functionality <a href="http://catalog.bocajava.com/coffee/coffee">here</a> by hovering over &#8216;details&#8217; or clicking on &#8216;buy now&#8217;).<br />
<span id="more-166"></span><br />
 In case you haven&#8217;t tried this before &#8211; IE7 and IE8 generally do not react well to fading PNGs, putting a black background under the portions of the PNG which have alpha transparency &#8211; and generally making the element look less than ideal.</p>
<p>Luckily, there is a fairly easy workaround for this situation as long as you are not trying to do too much with the pngs.  It will even fix pngs in IE6 for you too (in fact, that is the real purpose of the script).   That workaround is the jQuery ifixpng2 plugin, written by Yereth and available <a href="http://plugins.jquery.com/project/iFixPng2">here</a>.</p>
<p>All you need to do is install the plugin and use it on the PNG containing objects (images or backgrounds) that you would like to fade.  There are two changes you will need to make however:</p>
<p>Line 64 looks like this:</p>
<pre lang="php">ltie7	: $.browser.msie &amp;&amp; $.browser.version &lt; 7</pre>
<p>You&#8217;ll want to change the 7 to a 9 (in hopes that this is fixed for good by IE 9, otherwise you&#8217;ll need to go back and change it to 10 later).</p>
<p>The other change you need to make is on Line 149, which looks like this:</p>
<pre lang="php">for (prop in expr) elem.style.setExpression(prop, expr[prop], 'JavaScript');</pre>
<p>Internet Explorer 8 doesn&#8217;t support setExpression, so it will throw an error, unless you add an if statement to keep it from running:</p>
<pre lang="php">if( $.browser.msie &amp;&amp; $.browser.version &lt; 8 ) {
for (prop in expr) elem.style.setExpression(prop, expr[prop], 'JavaScript');
}</pre>
<p>That&#8217;s all there is to it.  Happy fading!</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2009/10/fading-pngs-in-internet-explorer-7-and-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Quick Code Igniter and JQuery Ajax Tutorial</title>
		<link>http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/</link>
		<comments>http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 04:18:01 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[code igniter]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrforbes.com/?p=138</guid>
		<description><![CDATA[Where I explain how to use the Code Igniter php framework and jquery to do some ajax. Re-post of 8/07 article. <a href="http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>This tutorial assumes a basic working knowledge of Code Igniter.  If you have never used CI before, please refer to the <a href="http://codeigniter.com/user_guide/">framework documentation</a></em></p>
<p>In the old days (2 years ago), working the Javascript magic to create a cool AJAX based event took a fairly decent working knowledge of the mechanisms behind the process.<br />
<span id="more-138"></span><br />
With the increasing popularity of Javascript libraries however, this type of functionality became available to the web site hobbyist, and was made much easier for the web site professional.</p>
<p>The following step-by-step tutorial will show you how to combine the power of <a href="http://www.jquery.com">JQuery</a> (a javascript library that weighs in at about 20k) with <a href="http://www.codeigniter.com">Code Igniter</a> (a PHP framework based on the MVC design pattern) to quickly and painlessly pass a record ID through the javascript and over to the server, where it will be passed to a mysql database, used to retrieve some data, and sent back to the page for display.</p>
<p><strong>Step 1</strong><br />
We begin by assuming that you have a div with an id of content, which is where you would like your freshly retrieved data to display, once it has been freshly retrieved.  For this exercise, you have already taken an action to call your javascript function with a record ID parameter.</p>
<p>The first thing you need to do, is make sure JQuery is being loaded, and to create a function for your AJAX request.</p>
<pre lang="php">
<script language="javascript" src="/path_to_jquery/jquery.js" ></script>
<script language="javascript">
 function get_record_id(record_id)
     {
     }
</script>
</pre>
<p><strong>Step 2:</strong><br />
Next, youll use the JQuery function load, and attach it to your content div:</p>
<pre lang="php">
function get_record_id(record_id) {
     $('#content').load()
}
</pre>
<p><strong>Step 3:</strong><br />
The load function accepts three arguments.  The page to be called on the other side of the HTTPRequest, the array to pass through the POST, and a callback function.  It looks like this:</p>
<pre lang="php">
function get_record_id(record_id) {
     $('#content').load(/controller/method,p,function(str){

	 });
}
</pre>
<p>Lets go back to that.  Code Igniter URLs are created by calling the name of your controller, followed by the function inside the controller class that will handle your request.  If your server does not support mod-rewrite, you may also need to append an index.php to the beginning.  The str inside the callback function is the results of your AJAX request.  There isnt much use for the str when using the .load function, but it does come in handy using the other JQuery AJAX functions &#8211; $.post and $.get, which I assume are self explanatory.</p>
<p><strong>Step 4</strong></p>
<pre lang="php">
var p = {}; //instantiate the array
p[record_id] = record_id //assign your record_id variable to it.
</pre>
<p>Thats all there is to it.  Your final javascript function looks like this:</p>
<pre lang="php">
function get_record_id(record_id) {
     var p = {};
     p[record_id] = record_id
     $('#content').load(/controller/method,p,function(str){

     });
}
</pre>
<p><strong>Step 5</strong><br />
On the CI side, you have a controller and method setup something like this:</p>
<pre lang="php">
class Controller
{
   function Controller()
   {
       parent::CI;
   }

   function method()
   {
   }
}
</pre>
<p>The important part is the method() function, as it will contain some of the code we need to make things happen.</p>
<p><strong>Step 6</strong><br />
The first thing you need to do on the CI side is retrieve the value passed through the request object.  This is simple enough, using $_POST[record_id].  You also want to load up your database model so you can get the record out of your database.  So, well load the database library, and then load the actual model.  Then, we want to send the record ID to the database, get the resulting data, and pass it back out to the request.  our function starts to look like its doing something useful pretty quickly.</p>
<pre lang="php">
function method()
{
   $record_id = $_POST[record_id];
   //set the record ID
   $this->load->library(database);
   //load the database library to connect to your database
   $this->load->model(records);
   //inside your system/application/models folder, create a model based on the procedure
   //outlined in the CI documentation
   $results = $this->records->get_record($record_id);
   //get the record from the database
}
</pre>
<p><strong>Step 7</strong><br />
At this point, we need to go into our records.php file in the model folder.  Since Code Igniter uses a Model-View-Controller structure, database activity, server-side processing, and client-side display should be as separate from one another as possible.  You dont NEED to do this for Code Igniter to do its thing, but its good practice.</p>
<p>Inside the records.php file, well create a method called get_record to match the method referenced above.  Well use it to get a record by its primary key of ID, put the resulting data into an array, and send it back to the controller, out to the view, and ultimately into the content div we started with.</p>
<pre lang="php">
function get_record($record_id)
{
   $this->db->where(ID,$record_id);
   //we want the row whose ID matches the value were passing in
   $query = $this->db->get(record_table);
   //get the table and put it into an object named $query
   $row = $query->row();
   //gets the first row of the resulting dataset.  In this case, only 1 row will ever be returned
   $results[record][$row->ID][name] = $row->name;
   //here, we create a multi-dimensional array holding the returned values
   //based on the key.
   return $results;
   //send the record back to the controller
}
</pre>
<p>The trickiest part of this section is the array.  It seems pretty complex from here, but youll see soon enough how it breaks down into something more manageable as we go along.</p>
<p><strong>Step 8</strong><br />
Were back to the controller again, and we have one more line to add &#8211; this time to pass the resulting data into a view to be formatted and printed to the content div.  The whole method() function now looks like this:</p>
<pre lang="php">
function method()
{
   $ID = $_POST[record_id];  //set the record ID
   $this->load->library(database);
   //load the database library to connect to your database
   $this->load->model(records);
   //inside your system/application/models folder, create a model based on
   //the procedure outlined in the CI documentation
   $results = $this->records->get_record($record_id);
   //get the record from the database
   $this->load->view(AJAX_record,$results);
}
</pre>
<p><strong>Step 9</strong><br />
The AJAX_record.php file should be in your system/application/views folder.  Keep in mind, that when you pass an array to a view (in this case the $results array), it will be exploded inside the view.  So, the path to your record is now $record, instead of $results[record].  Also inside will be your standard HTML markup, and something like this:</p>
<pre lang="php">
< ?php foreach($record as $id=>$value) { ?>
     The name associated with this record is: < ?php print $value[name];?>
< ?php } ?>
</pre>
<p>This output is what php is sending to the request object, and is also what gets loaded into the content div.  Code Igniter and JQuery make it that easy to dynamically load data using AJAX.</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/feed/</wfw:commentRss>
		<slash:comments>58</slash:comments>
		</item>
	</channel>
</rss>

