<?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>Stephen Gray&#039;s dev blog &#187; javascript</title>
	<atom:link href="http://blog.s-gray.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.s-gray.com</link>
	<description>e-commerce and integrations</description>
	<lastBuildDate>Sun, 17 Jan 2010 09:32:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery retrieving the data from an AJAX call into the global scope</title>
		<link>http://blog.s-gray.com/2009/07/27/jquery-retrieving-the-data-from-an-ajax-call-into-the-global-scope/</link>
		<comments>http://blog.s-gray.com/2009/07/27/jquery-retrieving-the-data-from-an-ajax-call-into-the-global-scope/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 09:06:16 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://colourgray.wordpress.com/?p=133</guid>
		<description><![CDATA[You make an AJAX call within a jQuery function and you want the call to be synchronous so that you can use the data from the call in the function scope. This tutorial will show you how.]]></description>
			<content:encoded><![CDATA[<p>Another quick one. Reading around it seems to be an issue. You make an AJAX call within a function and you want the call to be synchronous so that you can use the data from the call in the function scope.</p>
<p>AJAX calls are obviously asynchronous. jQuery has a &#8216;async: false&#8217; option in AJAX calls but it is ignored by most (maybe all?) browsers. Using this method you can force a synchronous request and retrieve that data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p133code1'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1331"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p133code1"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> myVariable <span style="color: #339933;">=</span> $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'someScript.php'</span><span style="color: #339933;">,</span>
        async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myVariable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So .responseText retrieves the text value of the response from the AJAX call. And because we&#8217;re assigning to a variable we are forcing a synchronous request. You can now use that variable as you see fit.</p>
<p>This causes an issue when you are trying to retrieve a JSON object from an AJAX call into the function scope. The best way I&#8217;ve found so far is to retrieve the JSON as a string and use a jquery JSON plugin to convert it into an accessible object. Or you could just use the &#8216;eval&#8217; method.</p>
<p>Stephen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.s-gray.com/2009/07/27/jquery-retrieving-the-data-from-an-ajax-call-into-the-global-scope/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery wildcard selectors &#8211; update</title>
		<link>http://blog.s-gray.com/2009/04/24/jquery-wildcard-selectors-update/</link>
		<comments>http://blog.s-gray.com/2009/04/24/jquery-wildcard-selectors-update/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 09:16:58 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[wildcard]]></category>

		<guid isPermaLink="false">http://colourgray.wordpress.com/?p=123</guid>
		<description><![CDATA[On my first post on this blog I posted a small snippet to use a small regex pattern in jQuery selectors. This doesn't work anymore, go here for a working method: <a href="http://ropox.net/archives/1081">http://ropox.net/archives/1081</a>]]></description>
			<content:encoded><![CDATA[<p>Hello all,</p>
<p>On my first post on this blog I posted a small snippet to use a small regex pattern in jQuery selectors.</p>
<p>You can find the post here: <a href="http://colourgray.wordpress.com/2008/08/05/jquery-wildcard-selectors/">http://colourgray.wordpress.com/2008/08/05/jquery-wildcard-selectors/</a></p>
<p>I was reading through some comments and posts on some incoming links and it seems it wasn&#8217;t working for anyone anymore. I just tested it myself and unfortunately it&#8217;s true (at least with with jQuery v1.3.1). However! All is not lost, there is another method which I&#8217;ve tested from one of the incoming links to that post which you can find here:</p>
<p><a href="http://ropox.net/archives/1081">http://ropox.net/archives/1081</a></p>
<p>Cheers ropox!</p>
<p>Stephen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.s-gray.com/2009/04/24/jquery-wildcard-selectors-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SproutCore: a proper javascript framework</title>
		<link>http://blog.s-gray.com/2008/09/17/sproutcore-a-proper-javascript-framework/</link>
		<comments>http://blog.s-gray.com/2008/09/17/sproutcore-a-proper-javascript-framework/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:03:49 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Dev thoughts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[sproutcore]]></category>

		<guid isPermaLink="false">http://colourgray.wordpress.com/?p=107</guid>
		<description><![CDATA[I wrote a while ago about creating a client side MVC system in javascript.
Yesterday at one of the talks SproutCore was mentioned. I had a look today and it looks very promising. It&#8217;s a full framework designed to create full &#8216;thick client&#8217; javascript applications. The core of it was written in Ruby and I haven&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a while ago about<a href="http://colourgray.wordpress.com/2008/08/28/jquery-create-a-client-side-mvc/"> creating a client side MVC system</a> in javascript.</p>
<p>Yesterday at one of the talks <a href="http://www.sproutcore.com/">SproutCore</a> was mentioned. I had a look today and it looks very promising. It&#8217;s a full framework designed to create full &#8216;thick client&#8217; javascript applications. The core of it was written in Ruby and I haven&#8217;t looked into whether you have to know much Ruby to develop in it or not. It reminds me of <a href="http://www.symfony-project.org">symfony</a> because you are given a number of build tools to use on the command line for creating controllers, new projects, models etc etc.</p>
<p>I think I&#8217;m falling in love with the general idea and it&#8217;s great to see someone has actually implemented it quite well (from the looks of it). I haven&#8217;t had the chance to try it out yet but I&#8217;m thinking of using it on a number of personal projects so we&#8217;ll see how it goes!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.s-gray.com/2008/09/17/sproutcore-a-proper-javascript-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery create a client-side MVC</title>
		<link>http://blog.s-gray.com/2008/08/28/jquery-create-a-client-side-mvc/</link>
		<comments>http://blog.s-gray.com/2008/08/28/jquery-create-a-client-side-mvc/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 16:59:15 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://colourgray.wordpress.com/?p=85</guid>
		<description><![CDATA[Some concepts for creating a client side MVC (or VC in this case) framework.]]></description>
			<content:encoded><![CDATA[<p>A while ago a colleague suggested to me the idea of having a client-side MVC (Model View Controller) framework. At first I didn&#8217;t like the idea. I have always thought that all client-side code should be seen as one layer, one collection of files that all act as one set of functionality. Whether that be AJAX calls or general functions etc etc.</p>
<p>Then I began to think about it. We already know that a client-side MVC is possible, but I can now see why it would be useful. But it wouldn&#8217;t be the traditional MVC architecture, it would really only be VC, the view and controller layers. And that&#8217;s all it can ever really be. Javascript can never directly access the model in a web application environment.</p>
<p>I decided that a VC client-side system would still be useful and I&#8217;m probably going to develop one for future use. I haven&#8217;t got round to doing this yet but these are my ideas.</p>
<p>Firstly, looking at a few of our sites, I could quickly see that there were many javascript files being loaded per page as they were needed. The number of files could range from 5 to 10 or even more. So we need a way of dynamically loading javascript files onto the page as and when they are needed, on the fly. I initially thought that it wasn&#8217;t possible to dynamically load javascript files from within a javascript page but I Googled around a bit and when I found the solution it was painfully obvious: you simply load more  tags in the  section of the HTML.</p>
<p>So with that in mind, lets create a simple front controller. What does it need to do? It needs to have some basic configuration for the MVC; where the root of the files are etc. It needs to have the aforementioned function to load external javascript files into the current page. It needs to have an initialisation function which kicks the MVC off:</p>
<p>mvc.init.js</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p85code2'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p852"><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
</pre></td><td class="code" id="p85code2"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * client MVC initialiser
 */</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * wheres are the MVC files?
 *
 * @access public
 * @var    string
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> MVCRoot <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/js/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * initialise the controller
 */</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    loadJS<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mvc.controller'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Controller.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * load an external js file by appending
 * script tag as child to &amp;lt;head&amp;gt; section
 *
 * @var    fileName name of file to load
 * @return bool true on success, false on no fileName given
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> loadJS<span style="color: #009900;">&#40;</span>fileName <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileName <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;script type=&quot;text/javascript&quot; src=&quot;'</span><span style="color: #339933;">+</span>MVCRoot<span style="color: #339933;">+</span>fileName<span style="color: #339933;">+</span><span style="color: #3366CC;">'.js&quot;&amp;gt;&amp;lt;/script&amp;gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'head'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ok most of that should make sense. The loadJS() function is fairly straight forward, it simply builds a HTML &lt;script&gt; tag for the new javascript file and appends it to the &lt;head&gt; tag of the HTML. The $(document).ready function then uses this function to &#8216;include&#8217; the main controller file (which will be /js/mvc.controller.js according to the current config). Now that the init function can call methods of the Controller class it calls the main controller&#8217;s initialisation method.</p>
<p>As an initial idea, the main controller could start off looking like this:</p>
<p>mvc.controller.js</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p85code3'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p853"><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
</pre></td><td class="code" id="p85code3"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * client main controller
 */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Controller <span style="color: #339933;">=</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/**
     * holds the current page's type
     * used for loading required js files etc
     *
     * @var    string
     * @access protected
     */</span>
    <span style="color: #003366; font-weight: bold;">var</span> _pageType <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * main controller init function
     * gets current page type
     * includes required js files
     */</span>
    <span style="color: #003366; font-weight: bold;">var</span> init <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        loadJS<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mvc.controller.ajax'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Controller._getPageType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * logic to include required files dependent on page
         * type etc etc
         */</span>
        ...
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * returns the page type for current URI
     *
     * @return void
     */</span>
    <span style="color: #003366; font-weight: bold;">var</span> _getPageType <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> currentURI <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span><span style="color: #339933;">;</span>
        Controller._pageType <span style="color: #339933;">=</span> ControllerAjax.<span style="color: #660066;">getPageType</span><span style="color: #009900;">&#40;</span>currentURI<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Again this is mostly basic stuff and it definitely hasn&#8217;t been tested but I think the general idea is in the right direction. I think now that I can visualise how to seperate out javascript objects more clearly I can see how this would benefit a javascript heavy web site/application.</p>
<p>Even though web frameworks have been gaining more and more popularity over recent years I hadn&#8217;t thought of applying the same methodical approach to client side coding until my colleague suggested this idea. I think if you think about it and you&#8217;re thinking like I was, you can quickly warm to the idea as I have. Almost every day you can read a new article about how in development, we need to be separating out our different types of logic and our layers. Javascript is of course just another language, so why should it not get the same treatment? <img src='http://blog.s-gray.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.s-gray.com/2008/08/28/jquery-create-a-client-side-mvc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery wildcard selectors</title>
		<link>http://blog.s-gray.com/2008/08/05/jquery-wildcard-selectors/</link>
		<comments>http://blog.s-gray.com/2008/08/05/jquery-wildcard-selectors/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 16:02:13 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[wildcard]]></category>

		<guid isPermaLink="false">http://colourgray.wordpress.com/?p=13</guid>
		<description><![CDATA[UPDATE: The below method no longer works! Go here to see a tested, working method: http://ropox.net/archives/1081
This is the first tutorial on my blog. We&#8217;re going to start with something simple and it&#8217;s only useful if you use jQuery.
When I started using jQuery I found it hard to find information on using wildcards when selecting DOM [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="color:#ff0000;">UPDATE:</span> The below method no longer works! Go here to see a tested, working method: <a href="http://ropox.net/archives/1081">http://ropox.net/archives/1081</a></strong></p>
<p>This is the first tutorial on my blog. We&#8217;re going to start with something simple and it&#8217;s only useful if you use jQuery.</p>
<p>When I started using jQuery I found it hard to find information on using wildcards when selecting DOM elements.</p>
<p>After a while you get to understand that jQuery allows the use of regular expressions in a lot of places, I.E. when selecting like this:</p>
<p><code>$('#myElement').html();</code></p>
<p>So with this in mind, you can use \\S* to make wildcard selections, I.E.:</p>
<p><code>$('#myEle\\S*').each();</code></p>
<p>Stephen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.s-gray.com/2008/08/05/jquery-wildcard-selectors/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

