<?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/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Sencha &#8211; Matt Goldspink</title>
	<atom:link href="https://www.mattgoldspink.co.uk/category/sencha/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mattgoldspink.co.uk</link>
	<description>Salesforce Developer with Vlocity Inc</description>
	<lastBuildDate>Sun, 13 Jan 2019 21:36:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.4.4</generator>
<site xmlns="com-wordpress:feed-additions:1">45832172</site>	<item>
		<title>Senchacon 2013: Clean and Maintainable Ext JS</title>
		<link>https://www.mattgoldspink.co.uk/senchacon-2013/</link>
					<comments>https://www.mattgoldspink.co.uk/senchacon-2013/#respond</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Thu, 25 Jul 2013 15:21:59 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15995</guid>

					<description><![CDATA[<p>Clean and Maintainable Ext JS: Tools and Patterns for Greater Code Clarity This is the talk I gave at Senchacon 2013 based on my experiences of working on large Ext.js and Sencha Touch based projects in Morgan Stanley and Causata. Slides Audio Video no longer available</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/senchacon-2013/">Senchacon 2013: Clean and Maintainable Ext JS</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2>Clean and Maintainable Ext JS: Tools and Patterns for Greater Code Clarity</h2>



<p>This is the talk I gave at Senchacon 2013 based on my experiences of working on large Ext.js and Sencha Touch based projects in Morgan Stanley and Causata.</p>



<h2>Slides</h2>



<script async="" class="speakerdeck-embed" data-id="d3540680d1e00130c0b5026b4eb85e5a" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"></script>



<h2>Audio</h2>



<p>Video no longer available</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/senchacon-2013/">Senchacon 2013: Clean and Maintainable Ext JS</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/senchacon-2013/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15995</post-id>	</item>
		<item>
		<title>Simplifying Grunt Jasmine tests for Ext.js</title>
		<link>https://www.mattgoldspink.co.uk/simplifying-grunt-jasmine-tests-on-an-ext-js-project/</link>
					<comments>https://www.mattgoldspink.co.uk/simplifying-grunt-jasmine-tests-on-an-ext-js-project/#respond</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Tue, 26 Mar 2013 08:11:06 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15828</guid>

					<description><![CDATA[<p>Are you wanting to unit test you Ext.js code but not sure where to start? Do you feel frustrated that everytime you get a bug raised you don&#8217;t know how to apply TDD techniques to your JavaScript because it&#8217;s just &#8220;not the same kind of code&#8221; as your Java, Ruby, .Net? Since I&#8217;ve worked with ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/simplifying-grunt-jasmine-tests-on-an-ext-js-project/">Simplifying Grunt Jasmine tests for Ext.js</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Are you wanting to unit test you Ext.js code but not sure where to start? Do you feel frustrated that everytime you get a bug raised you don&#8217;t know how to apply TDD techniques to your JavaScript because it&#8217;s just &#8220;not the same kind of code&#8221; as your Java, Ruby, .Net?</p>
<p>Since I&#8217;ve worked with Ext.js and Sencha Touch, testing has long been an issue that I&#8217;ve fought with. Initially I did no testing other than opening the browser hitting refresh and retracing my steps. However last year I went to Full Frontal and attended Rebecca Murphy&#8217;s talk (<a href="http://www.youtube.com/watch?v=OzjogCFO4Zo">video</a> &amp; <a href="https://speakerdeck.com/rmurphey/writing-testable-javascript">slides</a>) and workshop on how to write testable JavaScript. Since then I&#8217;ve been cranking out test, after test, after test, and it&#8217;s improved the quality of code and its stability drastically.</p>
<p>However it did take me a while to understand how to setup Jasmine for an Ext.js project, so I thought something could be done about this to make life simpler. So I created a new Grunt plugin:</p>
<p><strong><a href="https://npmjs.org/package/grunt-sencha-jasmine">grunt-sencha-jasmine</a></strong></p>
<p>It extends the <a href="https://npmjs.org/package/grunt-contrib-jasmine">grunt-contrib-jasmine</a> so should be compatible with all options there, but it sets up a different template which just needs to know the location of the Ext framework and also any Ext.Loader.setPath() configurations.</p>
<p>To help show it and other ways to use Grunt on Ext.js projects I&#8217;ve setup an example Git project for people to clone and play with:</p>
<p><strong><a href="https://github.com/mattgoldspink/grunt-extjs-example">https://github.com/mattgoldspink/grunt-extjs-example</a></strong></p>
<p>This project comes with a nice simple Grunt file and <strong>ONE</strong> <a href="https://github.com/mattgoldspink/grunt-extjs-example/blob/master/tests/specs/app/model/Station.js">Jasmine test</a>. If you&#8217;re going to start testing, start <strong>tiny</strong>, don&#8217;t try cover your whole code base. If you can write this one unit test, let this be it!</p>
<p>I hope people find this useful and please feel free to give any feedback.</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/simplifying-grunt-jasmine-tests-on-an-ext-js-project/">Simplifying Grunt Jasmine tests for Ext.js</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/simplifying-grunt-jasmine-tests-on-an-ext-js-project/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15828</post-id>	</item>
		<item>
		<title>Using grunt with Ext.js 4.x projects</title>
		<link>https://www.mattgoldspink.co.uk/using-grunt-with-ext-js-4-x-projects/</link>
					<comments>https://www.mattgoldspink.co.uk/using-grunt-with-ext-js-4-x-projects/#comments</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Sun, 10 Feb 2013 16:29:00 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15801</guid>

					<description><![CDATA[<p>17 March 2013 &#8211; Updated to included the new simpler syntax of 0.6.0 So I stumbled across a thread last week on the Sencha forums marked under their &#8220;Trending Threads&#8221;. It&#8217;s title: Sencha Cmd Needs Some F*****G Coffee!!! It&#8217;s an eye catching title! Specifically there was one post there which said: I really would love ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/using-grunt-with-ext-js-4-x-projects/">Using grunt with Ext.js 4.x projects</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><b>17 March 2013 &#8211; Updated to included the new simpler syntax of 0.6.0</b></p>
<p>So I stumbled across a thread last week on the Sencha forums marked under their &#8220;Trending Threads&#8221;. It&#8217;s title:</p>
<p><a href="http://www.sencha.com/forum/showthread.php?254824">Sencha Cmd Needs Some F*****G Coffee!!!</a></p>
<p>It&#8217;s an eye catching title! Specifically there was <a href="http://www.sencha.com/forum/showthread.php?254824-Sencha-Cmd-Needs-Some-F*****G-Coffee!!!&#038;p=936043&#038;viewfull=1#post936043">one post there which said</a>:</p>
<blockquote><p>I really would love to see someone starting a yeoman/grunt based community replacement for the Sencha CMD</p></blockquote>
<p>Having written a POC of this at an old company I thought it right that I make a proper open source version and share it. So last week I hastily hacked up my first version and very prematurely posted it <a href="http://www.sencha.com/forum/showthread.php?254824-Sencha-Cmd-Needs-Some-F*****G-Coffee!!!&#038;p=936364&#038;viewfull=1#post936364">to the thread</a>, <a href="https://twitter.com/mattgoldspink/status/299232964364103680">twitter</a> and <a href="http://www.mattgoldspink.co.uk/2013/02/06/fed-up-sencha-cmd-want-to-use-grunt-with-ext-js-and-sencha-touch/">here</a>. It was crap, worked only on my project and was riddled with a lot of bad code and bugs (apologies if you tried it and hit problems).</p>
<p>Over the last few days I decided to tidy up the code, add some tests, and road test it on a few more project. I&#8217;m now happy to say that it&#8217;s working pretty solidly on Ext.js and Sencha Touch projects. So I&#8217;d like to run through the simple setup on an existing project. My choice was the Pandora MVC project that Sencha provide as their <a href="http://docs.sencha.com/ext-js/4-1/#!/guide/mvc_pt1">MVC walkthrough application</a>, but the following steps should work just as well on any existing project your have.</p>
<p><i>If you get stuck or have feedback please leave a comment below.</i></p>
<h2>Getting started on an existing project</h2>
<p><i>I&#8217;m assuming you have node.js installed, if not grab a copy from <a href="http://nodejs.org/">http://nodejs.org/</a></i></p>
<p>First thing we need to do is add a package.json file to your project which can be done by running the following on the command line:</p>
<pre class="brush: bash; title: ; notranslate">
$ cd project/root/src/dir
$ npm init
</pre>
<p>This will ask a few questions about the project name, version, test commands, etc. For the ones where you know the data fill it in, for the others just hit enter and accept the defaults &#8211; they can always be changed later.</p>
<p>Next up you need to get the latest version of Grunt. To do this run the following:</p>
<pre class="brush: bash; title: ; notranslate">
$ npm uninstall -g grunt
$ npm install -g grunt-cli
$ npm install grunt --save-dev
</pre>
<p><i>You may need to run the first two as root, depending on your OS and previous installs</i><br />
This should add grunt locally to your project. Running grunt should give:</p>
<pre class="brush: bash; title: ; notranslate">
$ grunt
A valid Gruntfile could not be found. Please see the getting started guide for
more information on how to configure grunt: http://gruntjs.com/getting-started
Fatal error: Unable to find Gruntfile.
</pre>
<p>Like Ant, make, etc they all need a file which defines some tasks to run. For grunt this file should be called <code>Gruntfile.js</code> and be in your project root. Here&#8217;s a simple one to get started with:</p>
<pre class="brush: jscript; title: ; notranslate">
module.exports = function(grunt) {

    grunt.initConfig({
        
    });

    grunt.registerTask('default', []);

};
</pre>
<p>Hopefully if you run grunt again with the above in a file called <code>Gruntfile.js</code> you should see:</p>
<pre class="brush: bash; title: ; notranslate">
$ grunt

Done, without errors.
</pre>
<h2>Adding the sencha_dependencies task</h2>
<p>To add the <code>sencha_dependencies</code> task to your project you need to do two things. First add it as a dependency via NPM:</p>
<pre class="brush: bash; title: ; notranslate">
$ npm install grunt-sencha-dependencies --save-dev
</pre>
<p>And then we need to add it to our <code>Gruntfile.js</code></p>
<pre class="brush: jscript; title: ; notranslate">
module.exports = function(grunt) {

    grunt.initConfig({
        sencha_dependencies: {
          app: {
            options : {
              appFile: 'app/app.js',
              pageToProcess: 'index.html'
            }
          }
        }
    });

    grunt.loadNpmTasks('grunt-sencha-dependencies');

    grunt.registerTask('default', ['sencha_dependencies:app']);

};
</pre>
<p>For your project you&#8217;ll need to tweak the following:</p>
<ul>
<li><code>appFile</code> &#8211; This should be the relative path of the file containing your call to <code>Ext.application</code> from your html file</li>
<li><code>pageToProcess</code> &#8211; This should be the name of the html file which we&#8217;ll use to run your app in headlessly and figure out what files get loaded and in what order you need them in production</li>
<li><code>pageRoot</code> &#8211; By default the task assumes your html pages live in the root of your project. If thats not the case and they live under some subdirectory, then you should set this property to be your subdirectory</li>
</ul>
<p>With the file now updated with the above try running:</p>
<pre class="brush: bash; title: ; notranslate">
$ grunt
Running &amp;quot;sencha_dependencies:app&amp;quot; (sencha_dependencies) task
Processing Sencha app file index.html...
&gt;&amp;gt; Success! 230 files added to property sencha_dependencies_app

Done, without errors.
</pre>
<p>This tells you that it found 230 files which your app requires and it has now written it to a new global property called <code>sencha_dependencies_app</code></p>
<p>If you wanted to see what files it found then you can pass in the <code>--verbose</code> flag and you should see something like:</p>
<pre class="brush: bash; title: ; notranslate">
$ grunt --verbose
&amp;lt;snip&amp;gt;
&gt;&amp;gt; Success! 225 files added to property sencha_dependencies_app
Files are:
    ../libs/ext-4.1.1a/ext-debug.js
    ../libs/ext-4.1.1a/src/util/Observable.js
    ../libs/ext-4.1.1a/src/app/Controller.js
    ../libs/ext-4.1.1a/src/util/Filter.js
    ../libs/ext-4.1.1a/src/util/AbstractMixedCollection.js
    ../libs/ext-4.1.1a/src/util/Sorter.js
    ../libs/ext-4.1.1a/src/util/Sortable.js
    ../libs/ext-4.1.1a/src/util/MixedCollection.js
    ../libs/ext-4.1.1a/src/data/proxy/Proxy.js
    ../libs/ext-4.1.1a/src/data/Operation.js
&amp;lt;snip&amp;gt;
    ../libs/ext-4.1.1a/src/data/reader/Array.js
    ../libs/ext-4.1.1a/src/data/ArrayStore.js
    ../libs/ext-4.1.1a/src/data/Batch.js
    app/app.js

Done, without errors.
</pre>
<h2>Using this with our tasks to create output files</h2>
<p>On it&#8217;s own the above isn&#8217;t all that useful, but we can use the output of the task and pass it on to any other grunt task we want now. I think the most obvious example is wanting to concatenate and minify the code, in addition let&#8217;s also generate source maps to help with debugging in chrome. For this we need another task:</p>
<pre class="brush: bash; title: ; notranslate">
$ npm install grunt-contrib-uglify --save-dev
</pre>
<p>And in our <code>Gruntfile.js</code></p>
<pre class="brush: jscript; title: ; notranslate">
module.exports = function(grunt) {

    grunt.initConfig({
        sencha_dependencies: {
          app: {
            options : {
              appFile: 'app/app.js',
              pageToProcess: 'index.html'
            }
          }
        },
        uglify: {
          app: {
            options: {
              sourceMap: 'build/source-map.js'
            },
            files: {
              'build/app.min.js': ['&lt;%= sencha_dependencies_app %&gt;']
            }
          }
        }
    });
    
    
    grunt.loadNpmTasks('grunt-sencha-dependencies');
    grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.registerTask('default', ['sencha_dependencies:app', 'uglify:app]);

};
</pre>
<p>Now if you run grunt, you&#8217;ll see:</p>
<pre class="brush: bash; title: ; notranslate">
$ grunt
Running &amp;quot;sencha_dependencies:app&amp;quot; (sencha_dependencies) task
Processing Sencha app file index.html...
&gt;&amp;gt; Success! 230 files added to property sencha_dependencies_app

Running &amp;quot;uglify:app&amp;quot; (uglify) task
Source Map &amp;quot;build/source-map.js&amp;quot; created.
File &amp;quot;build/app.min.js&amp;quot; created.
Uncompressed size: 3488667 bytes.
Compressed size: 262655 bytes gzipped (890454 bytes minified).

Done, without errors.
</pre>
<p>The uglify task uses the files output from the <code>sencha_dependencies</code> task via the property it created:</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;%= sencha_dependencies_app %&gt;
</pre>
<p>This is special templating syntax which means the value in the property won&#8217;t be resolved until it&#8217;s needed. After that we can see uglify created a <code>source-map.js</code> which will work with debuggers that support them (chrome and some versions of firebug), and finally it concated and minifed all our files into <code>dest/app.min.js</code></p>
<h2>Using the minified file in our production application</h2>
<p>Now we have a minified set of all the files we should make sure it gets used when we go live. I&#8217;ve seen teams in the past create an <code>index-development.html</code> and an <code>index-production.html</code>. This is one option, but grunt supports  a copy tasks which can do any replacement we want, so let&#8217;s keep one index.html and assume we want to update it into our <code>build</code> output directory.</p>
<pre class="brush: bash; title: ; notranslate">
$ npm install grunt-contrib-copy --save-dev
</pre>
<p>Our grunt task will look like:</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;snip&gt;
    grunt.initConfig({
        &lt;snip&gt;
        copy: {
          app: {
            files: [
              {expand: true, src: ['resources/css/ext-all.css'], dest: 'build/', cwd: '../libs/ext-4.1.1a/',},
              {expand: true, src: ['resources/themes/images/**'], dest: 'build/', cwd: '../libs/ext-4.1.1a/',},
              {src: ['index.html'], dest: 'build/'}
            ],
            options: {
              processContent: function(content, filePath) {
                // process only the index.html content
                if (/index.html/.test(filePath)) {
                  // remove the ext script
                  content = content.replace(/&lt;script.*ext.js&quot;&gt;&lt;\/script&gt;/, '');
                  // now update the css location
                  content = content.replace(/\.\.\/libs\/ext-4.1.1a\//, '');
                  // now change our app.js to app.min.js
                  content = content.replace(/app\/app.js/, 'app.min.js');
                }
                return content;
              }
            }
          }
        }
    });

    grunt.loadNpmTasks('grunt-sencha-dependencies');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-copy');

    grunt.registerTask('default', ['sencha_dependencies:app', 'uglify:app', 'copy:app']);

};
</pre>
<p>This seems a little bit complicated but let me break it down:</p>
<pre class="brush: jscript; title: ; notranslate">
  files: [
    {expand: true, src: ['resources/css/ext-all.css'], dest: 'build/', cwd: '../libs/ext-4.1.1a/',},
    {expand: true, src: ['resources/themes/images/**'], dest: 'build/', cwd: '../libs/ext-4.1.1a/',},
    {src: ['index.html'], dest: 'build/'}
  ]
</pre>
<p>The above will move the CSS and image files over to our build directory, and in our addition it will move our index.html over to the build directory.</p>
<pre class="brush: jscript; title: ; notranslate">
  options: {
    processContent: function(content, filePath) {
      // process only the index.html content
      if (/index.html/.test(filePath)) {
        // remove the ext script
        content = content.replace(/&lt;script.*ext.js&quot;&gt;&lt;\/script&gt;/, '');
        // now change our app.js to app.min.js
        content = content.replace(/app\/app.js/, 'app.min.js');
        // now update the css location
        content = content.replace(/\.\.\/libs\/ext-4.1.1a\//, '');
      }
      return content;
    }
  }
</pre>
<p>This function is a hook the task provides to do&#8230; well pretty much anything! In my case I want to only process the index.html file and all I want to do is:</p>
<ul>
<li>remove the reference to the ext.js library JavaScript file (it&#8217;s now in a minified file)</li>
<li>swap the app.js with our new minified app.min.j</li>
<li>and also update the url location of our css file to point to our build version</li>
</ul>
<p>That&#8217;s it&#8230; this grunt file now does pretty much want any basic app needs.</p>
<h2>More information</h2>
<p>This is just a small walkthrough that will hopefully get people up and running with grunt on Ext.js projects. If you do have questions on the above please leave a comment below, or if you find a bug in the grunt-sencha-dependencies task please file an issue over at <a href="https://github.com/mattgoldspink/grunt-sencha-dependencies/issues?state=open">https://github.com/mattgoldspink/grunt-sencha-dependencies</a>. Similarly, if there&#8217;s features you think would be useful then please let me know (though bear in mind I want to keep this task simple and <i>just</i> deal with dependencies).</p>
<p>In addition if you want to see a fuller example Grunt file on a project please take a look at the Pandora MVC example in the project &#8211; <a href="https://github.com/mattgoldspink/grunt-sencha-dependencies/tree/master/test/integration/pandora-ext-4.1.1a">https://github.com/mattgoldspink/grunt-sencha-dependencies/tree/master/test/integration/pandora-ext-4.1.1a</a></p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/using-grunt-with-ext-js-4-x-projects/">Using grunt with Ext.js 4.x projects</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/using-grunt-with-ext-js-4-x-projects/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15801</post-id>	</item>
		<item>
		<title>Fed up Sencha Cmd? Want to use Grunt with Ext.js and Sencha Touch?</title>
		<link>https://www.mattgoldspink.co.uk/fed-up-sencha-cmd-want-to-use-grunt-with-ext-js-and-sencha-touch/</link>
					<comments>https://www.mattgoldspink.co.uk/fed-up-sencha-cmd-want-to-use-grunt-with-ext-js-and-sencha-touch/#comments</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Wed, 06 Feb 2013 19:05:08 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15794</guid>

					<description><![CDATA[<p>Are you fed up with figuring out all the bazillion commands in Sencha CMD? Do you just want to concat and minify your app without figuring out what the heck Sencha Cmd is asking you to do? Imagine if you could just use a tool like grunt, have it figure out the app dependencies correctly ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/fed-up-sencha-cmd-want-to-use-grunt-with-ext-js-and-sencha-touch/">Fed up Sencha Cmd? Want to use Grunt with Ext.js and Sencha Touch?</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Are you fed up with figuring out all the bazillion commands in Sencha CMD? Do you just want to concat and minify your app without figuring out what the heck Sencha Cmd is asking you to do?</p>
<p>Imagine if you could just use a tool like grunt, have it figure out the app dependencies correctly and allow you to use any other grunt plugin.</p>
<p>I introduce my very basic grunt plugin: <a href="https://npmjs.org/package/grunt-sencha-dependencies">https://npmjs.org/package/grunt-sencha-dependencies</a></p>
<p>It just does one thing, given a js file with an Ext.application in it, what are all the other is files it needs in order to run.</p>
<p>It likely has bugs, and will probably need more features, but for now it does the job. So please give it a go and give me some feedback on what else you think it needs.</p>
<p><a href="https://npmjs.org/package/grunt-sencha-dependencies">https://npmjs.org/package/grunt-sencha-dependencies</a></p>
<p><i>Update 10/02/2013 &#8211; See the follow up post <a href="http://www.mattgoldspink.co.uk/2013/02/10/using-grunt-with-ext-js-4-x-projects/">Using grunt with Ext.js 4.x projects</a> for a detailed example of this in use.</i></p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/fed-up-sencha-cmd-want-to-use-grunt-with-ext-js-and-sencha-touch/">Fed up Sencha Cmd? Want to use Grunt with Ext.js and Sencha Touch?</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/fed-up-sencha-cmd-want-to-use-grunt-with-ext-js-and-sencha-touch/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15794</post-id>	</item>
		<item>
		<title>Ext.js &#8211; Cancel a load on an Ext.data.Store</title>
		<link>https://www.mattgoldspink.co.uk/ext-js-cancel-a-load-on-an-ext-data-store/</link>
					<comments>https://www.mattgoldspink.co.uk/ext-js-cancel-a-load-on-an-ext-data-store/#comments</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Sun, 03 Feb 2013 00:59:00 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15788</guid>

					<description><![CDATA[<p>Have you ever wanted to cancel a load you initiated on an Ext.js Store? Unfortunately Ext.js doesn&#8217;t provide this functionality out of the box, but the below patches provide a way of doing this. By default the Ext.data.Store.load() call would return itself, but in the below code I changed it to return an Ext.data.Operation which ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/ext-js-cancel-a-load-on-an-ext-data-store/">Ext.js &#8211; Cancel a load on an Ext.data.Store</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Have you ever wanted to cancel a load you initiated on an Ext.js Store? Unfortunately Ext.js doesn&#8217;t provide this functionality out of the box, but the below patches provide a way of doing this.</p>
<p>By default the Ext.data.Store.load() call would return itself, but in the below code I changed it to return an Ext.data.Operation which I also added a new cancel() function on. To me it&#8217;s more useful to get the Operation back because I can also check the status of the load &#8211; See the Ext.data.Operation for other methods: <a href="http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Operation">http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Operation</a></p>
<h2>Why would I want this?</h2>
<ul>
<li>If you server takes a long time to load data (maybe they&#8217;re on a slow 3G connection) and the user wishes to stop the request</li>
<li>Your user misconfigures some properties in a form which setup the query with which the store loads it&#8217;s data. They may wish to change them and reload the store. This would allow the AJAX request to be aborted and the new load fired</li>
<li>Perhaps you have a large single page app and your user switches tabs and therefore there is no point in continuing with the request</li>
</ul>
<p>There&#8217;s probably more use cases, but these are the ones I&#8217;ve come across personally.</p>
<h2>How do I include this?</h2>
<p>Copy the below into new files and put them in your project &#8211; I&#8217;d recommend creating the directory structure to match the names:</p>
<h4><code>MG/data/OperationPatch.js</code></h4>
<pre class="brush: jscript; title: ; notranslate">
Ext.define('MG.data.OperationPatch', {
    override: 'Ext.data.Operation',
    request: null,

    setRequest: function(request) {
        this.request = request;
    },
    /**
     * Cancels the active operation
     */
    cancel: function() {
        if (this.request !== null) {
            Ext.Ajax.abort(this.request);
        }
    }
});
</pre>
<h4><code>MG/data/AbstractStorePatch.js</code></h4>
<pre class="brush: jscript; title: ; notranslate">
Ext.define('MG.data.AbstractStorePatch', {
    override: 'Ext.data.AbstractStore',

    load: function(options) {
        var me = this,
            operation,
            request;
 
        options = options || {};
 
        options.action = options.action || 'read';
        options.filters = options.filters || me.filters.items;
        options.sorters = options.sorters || me.getSorters();
 
        operation = new Ext.data.Operation(options);
 
        if (me.fireEvent('beforeload', me, operation) !== false) {
            me.loading = true;
            request = me.proxy.read(operation, me.onProxyLoad, me);
            if (!Ext.isEmpty(request)) {
                operation.setRequest(request);
            }
        }
 
        return operation;
    }
});
</pre>
<p>Then you&#8217;ll need to configure your app to load them on startup. I usually add the following at the top of the same file as my Ext.application definition</p>
<pre class="brush: jscript; title: ; notranslate">
Ext.Loader.setPath('MG', './MG'); // tweak this to be where you put the files
Ext.application({
    name: 'MyApp',
    requires: [
        'MG.data.OperationPatch',
        'MG.data.AbstractStorePatch'
    ],
    ... // rest of your Ext.application definition
});
</pre>
<h2>Using the new api in your app</h2>
<p>To cancel a request you&#8217;d simply do:</p>
<pre class="brush: jscript; title: ; notranslate">
var operation = myStore.load();
...
operation.cancel();
</pre>
<p>That&#8217;s all there is to it!</p>
<h3>Licensing</h3>
<p>For those that need a license to use stuff, the above are released under the <a href="http://mit-license.org/">MIT Open Source License</a>.</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/ext-js-cancel-a-load-on-an-ext-data-store/">Ext.js &#8211; Cancel a load on an Ext.data.Store</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/ext-js-cancel-a-load-on-an-ext-data-store/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15788</post-id>	</item>
		<item>
		<title>Simplifying view code in large Ext.js and Sencha apps</title>
		<link>https://www.mattgoldspink.co.uk/simplifying-view-code-in-large-ext-js-and-sencha-apps/</link>
					<comments>https://www.mattgoldspink.co.uk/simplifying-view-code-in-large-ext-js-and-sencha-apps/#respond</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Fri, 25 Jan 2013 07:30:00 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15770</guid>

					<description><![CDATA[<p>Do you ever feel like your view code in Ext.js is getting out of control? Do you end up copying and pasting button configs across multiple views? Imagine if you could control all your component config in fewer places, make it easier to share config, test components and update them. I&#8217;d like to introduce the ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/simplifying-view-code-in-large-ext-js-and-sencha-apps/">Simplifying view code in large Ext.js and Sencha apps</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Do you ever feel like your view code in Ext.js is getting out of control? Do you end up copying and pasting button configs across multiple views?</p>
<p>Imagine if you could <strong>control all your component config</strong> in fewer places, make it <strong>easier to share config</strong>, <strong>test components</strong> and update them.</p>
<p>I&#8217;d like to introduce the <strong>factory pattern</strong> as a simple, but effective, solution to this.</p>
<h1>View config explosion &#8211; A little background</h1>
<p>When I built my first Ext.JS application I found myself focussing on what screens I needed to implement, writing classes for each view and then wiring them together. This seemed natural!</p>
<p>Then our design team decided to change 90% of the toolbars in the application. </p>
<p><i>&#8220;Well I&#8217;ll add a custom class to the toolbar and apply it to all that need it&#8221;</i>. </p>
<p>Soon I was creating custom classes for every component and throwing them throughout all my views.</p>
<p>I started looking at the explosion of JSON config and began thinking <i>&#8220;I know no longer know what bits are relevant to what in the actual view&#8221;</i></p>
<p>A few years later a colleague showed me his simple yet effective way of dealing with this &#8211; the Factory Pattern &#8211; <i>thanks <a href="https://twitter.com/ian_alderson">Ian</a>!</i></p>
<h1>The Factory Pattern</h1>
<p>For those not familiar with the factory pattern, the idea is that you have a class whose sole purpose is to produce one thing. Think of a car factory &#8211; it&#8217;s purpose is to produce cars, it might produce variations in the colour or engine, but at the end of the day it will roll out cars. </p>
<p>A factory in programming is the same &#8211; for example an Ext.button.Button factory class&#8217;s sole purpose would be to produce buttons &#8211; maybe the buttons have different colours or text, but it just produces buttons.</p>
<h1>Refocusing our application design</h1>
<p>In order to benefit from the factory pattern I had to change my perspective &#8211; no longer did I just focus on what views my application needed, but I had to <b>look hard at what common components each view used</b>.</p>
<p>In fact the designer on the project created a PDF which showed all the components broken out from their views and scattered across a giant canvas. This made it much easier to see what was being used and the variations on it.</p>
<p><b>If you have a ux or design team I&#8217;d recommend getting them to create an image with just the components in it</b>.</p>
<h1>Creating our first factory</h1>
<p>So now with an idea of the kinds of components you need, let&#8217;s create a Button factory &#8211; to keep things simple my hypothetical app will have only 2 types of button:</p>
<ol>
<li>a green &#8220;confirm&#8221; style button: <button type="button" class="btn myapp-ok">Confirm</button></li>
<li>a red &#8220;cancel&#8221; style button: <button type="button" class="btn myapp-cancel">Cancel</button></li>
</ol>
<p>Styles for the colours will be driven by CSS, so they just need CSS classes to distinguish their type in the DOM:</p>
<pre class="brush: jscript; title: ; notranslate">
Ext.define(&quot;MyApp.view.factories.ButtonFactory&quot;, {
    makeOkStyleButton: function(text, clickHandler) {
      return new Ext.button.Button({
        cls: &quot;myapp-ok&quot;,
        handler: clickHandler
      });
    },

    makeCancelStyleButton: function(text, clickHandler) {
      return new Ext.button.Button({
        cls: &quot;myapp-cancel&quot;,
        handler: clickHandler
      });
    }
  }
);
</pre>
<p>Hopefully this is pretty straight forward to understand. One question came up while writing the above: </p>
<p><b>Should the factory create new instances of the component or return the JSON config?</b></p>
<p>I&#8217;ve thought this through more and reasoned that if I returned a new instance it should improve rendering performance &#8211; this is because if you use an xtype Sencha has to look up the type and perform an Ext.create to instantiate it. From Sencha&#8217;s own developers <a href="http://www.sencha.com/forum/showthread.php?166536-Ext.draw-Ext.create-usage-dropped-why&amp;p=700522&amp;viewfull=1#post700522">they recognise this is slow and removed all traces of Ext.create from their own code</a>. </p>
<p>Of course if you did want to get the config you could have another version of both methods which returns just the config, like:</p>
<pre class="brush: jscript; title: ; notranslate">
Ext.define(&quot;MyApp.view.factories.ButtonFactory&quot;, {
    makeOkStyleButtonConfig: function(text, clickHandler) {
       return {
        xtype: &quot;button&quot;,
        cls: &quot;myapp-ok&quot;,
        handler: clickHandler
      };
    },

    makeOkStyleButton: function(text, clickHandler) {
      return new Ext.button.Button(
          this.makeOkStyleButtonConfig(text, clickHandler)
      );
    },
...
</pre>
<h1>Using it in a view</h1>
<p>To use this in a view it&#8217;s simply a matter of injecting an instance of the factory and invoking it &#8211; for example:</p>
<pre class="brush: jscript; title: ; notranslate">
Ext.define(&quot;MyApp.view.ConfirmWindow&quot;, {
    extend: &quot;Ext.window.Window&quot;,
    requires: [
        &quot;MyApp.view.factories.ButtonFactory&quot;
    ],
    ...
    initComponent: function() {
        if (!Ext.isDefined(this.buttonFactory)) {
            throw 'You must inject a buttonFactory';
        }
        this.btns = [
            this.btnFactory.makeCancelStyleButton(&quot;Cancel&quot;, this.handleCancel),
            this.btnFactory.makeOkStyleButton(&quot;OK&quot;, this.handleOk)
        ]
    },
    handleCancel: function() {
       ...
    },
    handleOk: function() {
       ...
    }
});
</pre>
<p>The nice thing here is that we&#8217;ve made a relatively isolated set of components that we can easily test.</p>
<h1>Testing Factories in Jasmine</h1>
<p>Here&#8217;s a simple Jasmine spec to test our ButtonFactory:</p>
<pre class="brush: jscript; title: ; notranslate">
describe(&quot;Testing ButtonFactory&quot;, function() {

   beforeEach(function() {
       Ext.syncRequire(&quot;MyApp.view.factories.ButtonFactory&quot;);
   });

   it(&quot;Is loaded correctly&quot;, function() {
       // simple test to make sure it's loaded
       expect(MyApp.view.factories.ButtonFactory).not.toBe(undefined);
   });

   it(&quot;creates an ok button with my title and handler&quot;, function() {
       var factory = new MyApp.view.factories.ButtonFactory(),
           title = &quot;Test Title&quot;,
           handler = function() {},
           okBtn = factory.makeOkStyleButton(title, handler);

        expect(okBtn.getText()).toBe(title);
        expect(okBtn.handler).toBe(handler);
        expect(okBtn.hasCls(&quot;myapp-ok&quot;)).toBe(true);
   });

   // repeat above for other functions if needed

});
</pre>
<p>That&#8217;s it &#8211; not very exciting, but it&#8217;s a start. Where this begins becoming really useful is when you have factories that produce more complex UI components. For example, a factory that produces custom pop-ups or flyovers can now easily be developed and tested in isolation and then wired into your views.</p>
<style>
.btn {
  display: inline-block;
  *display: inline;
  padding: 4px 12px;
  margin-bottom: 0;
  *margin-left: .3em;
  font-size: 14px;
  line-height: 20px;
  color: #333333;
  text-align: center;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
  vertical-align: middle;
  cursor: pointer;
  background-color: #f5f5f5;
  *background-color: #e6e6e6;
  background-image: -moz-linear-gradient(top, #ffffff, #e6e6e6);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ffffff), to(#e6e6e6));
  background-image: -webkit-linear-gradient(top, #ffffff, #e6e6e6);
  background-image: -o-linear-gradient(top, #ffffff, #e6e6e6);
  background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
  background-repeat: repeat-x;
  border: 1px solid #bbbbbb;
  *border: 0;
  border-color: #e6e6e6 #e6e6e6 #bfbfbf;
  border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
  border-bottom-color: #a2a2a2;
  -webkit-border-radius: 4px;
     -moz-border-radius: 4px;
          border-radius: 4px;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff', endColorstr='#ffe6e6e6', GradientType=0);
  filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);
  *zoom: 1;
  -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
     -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
          box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
}.myapp-ok {
  color: #ffffff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
  background-color: #5bb75b;
  *background-color: #51a351;
  background-image: -moz-linear-gradient(top, #62c462, #51a351);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#62c462), to(#51a351));
  background-image: -webkit-linear-gradient(top, #62c462, #51a351);
  background-image: -o-linear-gradient(top, #62c462, #51a351);
  background-image: linear-gradient(to bottom, #62c462, #51a351);
  background-repeat: repeat-x;
  border-color: #51a351 #51a351 #387038;
  border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff62c462', endColorstr='#ff51a351', GradientType=0);
  filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);
}.myapp-ok:hover,
.myapp-ok:active {
  color: #ffffff;
  background-color: #51a351;
  *background-color: #499249;
}.myapp-cancel {
  color: #ffffff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
  background-color: #363636;
  *background-color: #222222;
  background-image: -moz-linear-gradient(top, #444444, #222222);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#444444), to(#222222));
  background-image: -webkit-linear-gradient(top, #444444, #222222);
  background-image: -o-linear-gradient(top, #444444, #222222);
  background-image: linear-gradient(to bottom, #444444, #222222);
  background-repeat: repeat-x;
  border-color: #222222 #222222 #000000;
  border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff444444', endColorstr='#ff222222', GradientType=0);
  filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);
}.myapp-cancel:hover,
.myapp-cancel:active {
  color: #ffffff;
  background-color: #222222;
  *background-color: #151515;7
}
</style>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/simplifying-view-code-in-large-ext-js-and-sencha-apps/">Simplifying view code in large Ext.js and Sencha apps</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/simplifying-view-code-in-large-ext-js-and-sencha-apps/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15770</post-id>	</item>
		<item>
		<title>Promises in Ext.js and Sencha Touch</title>
		<link>https://www.mattgoldspink.co.uk/promises-in-ext-js-and-sencha-touch/</link>
					<comments>https://www.mattgoldspink.co.uk/promises-in-ext-js-and-sencha-touch/#comments</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Mon, 21 Jan 2013 16:18:55 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=15749</guid>

					<description><![CDATA[<p>Do you feel like passing callbacks and setting scopes everywhere makes your code messy and difficult to follow? Promises can make your code more readable, easier to debug and understand, and also a easier to test. What is a promise though? Simple promises The idea of a promise is that you request someone to do ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/promises-in-ext-js-and-sencha-touch/">Promises in Ext.js and Sencha Touch</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Do you feel like passing callbacks and setting scopes everywhere makes your code messy and difficult to follow?</h2>
<p>Promises can make your code <strong>more readable</strong>, <strong>easier to debug</strong> and understand, and also a <strong>easier to test</strong>.</p>
<p>What is a promise though?</p>
<h1>Simple promises</h1>
<p>The idea of a promise is that you request someone to do something for you and in return they&#8217;ll give you a promise that it will be done and with which they will notify you. The simple flow looks like this:</p>
<pre class="brush: jscript; title: ; notranslate">
    // get promise
    var promise = getUserDetails(&quot;matt&quot;);
    // register a listener to be notified when it's done
    promise.then(function(mattsDetails) {
        // got some data back - now we can
        // deal with it
    });
</pre>
<p>Compare this to callbacks:</p>
<pre class="brush: jscript; title: ; notranslate">
    getUserDetails(&quot;matt&quot;, function(mattsDetails) {
        // got some data back - now we can
        // deal with it
    });
</pre>
<p>It&#8217;s pretty similar to promises right &#8211; so where&#8217;s this benefit I&#8217;m proposing? Well let&#8217;s look at a more complex, but realistic example of callbacks. Let&#8217;s imagine we have a backend that: </p>
<ul>
<li>Gets details about the current user</li>
<li>gets tweets for that user</li>
<li>renders those tweets into the dom</li>
<p>Oh and if an error happens at any point we should pop up a message:</ul>
</p>
<pre class="brush: jscript; title: ; notranslate">
    getUserDetails(&quot;matt&quot;, function(mattsDetails) {
        getTweetsForUser(mattsDetails, function(tweets) {
            addTweetsToUi(tweets);
        }, function(error) {
            showUiErrorMessage(error);
        });
    }, function(error) {
        showUiErrorMessage(error);
    });
</pre>
<p>I&#8217;ve seen this kind of code a lot (I&#8217;ve written this kind of code too), but you know what, we can simplify this a bit:</p>
<pre class="brush: jscript; title: ; notranslate">
    getUserDetails(&quot;matt&quot;, function(mattsDetails) {
        getTweetsForUser(mattsDetails, addTweetsToUi, showUiErrorMessage);
    }, showUiErrorMessage);
</pre>
<p>In fact I would usually pull out the functions to avoid nested function hell &#8211; it helps me understand the code better. So let&#8217;s see if this makes it easier to follow:</p>
<pre class="brush: jscript; title: ; notranslate">
    function handleMattsDetails(mattsDetails) {
        getTweetsForUser(mattsDetails, addTweetsToUi, showUiErrorMessage);
    };
    getUserDetails(&quot;matt&quot;, handleMattsDetails, showUiErrorMessage);
</pre>
<p>Hmm… To me this way of passing around callbacks still feels hard to understand. It doesn&#8217;t seem easy to follow the code</p>
<h1>The promises way</h1>
<p>Let&#8217;s just take a look at how a promises implemented version would look:</p>
<pre class="brush: jscript; title: ; notranslate">
    getUserDetails(&quot;matt&quot;)
           .then(getTweetsForUser)
           .then(addTweetsToUi, showUiErrorMessage);
</pre>
<p>Ok… this seems nice. Why is it better though? Well to me this just reads better, like a sentence:</p>
<p><i>&#8220;get user details (for) matt, then get tweets for (that) user, then add (the) tweets to (the) ui (or) show (a) ui error message&#8221;</i></p>
<p>I believe I could sit down at some code like this and understand what the code is trying to do with little cognitive effort. In addition I have only one error handler, this is quite a nice feature of promises, in that errors are propagated to the last error handler if they&#8217;ve not been handled before that. </p>
<p>So how does it work when writing the functions that return a promise?</p>
<h1>The promises API in detail</h1>
<p>Let&#8217;s investigate the <code>then</code> function we&#8217;ve been using:</p>
<pre class="brush: jscript; title: ; notranslate">
    promise.then(successHandler, errorHandler);
</pre>
<p>The Promise object will have a <code>then</code> function with which you register one or two callbacks. The first callback will be invoked when the promise succeeds and will pass in any data as the arguments. The second callback is invoked when an error occurs and can be passed an object with details of the error.</p>
<p>When you&#8217;re writing code that uses promises there is an expectation that you will return a promise to the invoker of your function. To write an Ext.js function which returns a promise your code would look like:</p>
<pre class="brush: jscript; title: ; notranslate">
    function getUserDetails(username) {
        var promise = new Promise();
        Ext.Ajax.request({
            url: &quot;/user/details&quot;,
            params: {
                username: username
            },
            success: function(response) {
                var userDetails = Ext.decode(response.responseText);
                promise.resolve(userDetails);
            },
            failure: function(response) {
                promise.reject(&quot;Could not get user details - server response was &quot; + response.status);
            }
        });
        return promise;
    }
</pre>
<p>The key bits here are the:</p>
<pre class="brush: jscript; title: ; notranslate">
    promise.resolve(userDetails);
</pre>
<p>which will cause the <code>successHandler</code> registered to be called and given the <code>userDetails</code> object. Should our AJAX request fail we do:</p>
<pre class="brush: jscript; title: ; notranslate">
    promise.reject(&quot;Could not get user details - server response was &quot; + response.status);
</pre>
<p>This causes the <code>errorHandler</code> to be called with the given message.</p>
<p>Let&#8217;s do a comparison to the version implemented with callbacks:</p>
<pre class="brush: jscript; title: ; notranslate">
    function getUserDetails(username, successHandler, errorHandler) {
        Ext.Ajax.request({
            url: &quot;/user/details&quot;,
            params: {
                username: username
            },
            success: function(response) {
                if (Ext.isFunction(successHandler)) {
                    var userDetails = Ext.decode(response.responseText);
                    successHandler(userDetails);
                }
            },
            failure: function(response) {
                if (Ext.isFunction(errorHandler)) {
                    errorHandler(&quot;Could not get user details - server response was &quot; + response.status);
                }
            }
        });
    }
</pre>
<p>To be honest there isn&#8217;t too much difference, but let&#8217;s list the differences that there are:</p>
<ol>
<li>Firstly we have to test for the two extra params and ensure they&#8217;re functions &#8211; technically we don&#8217;t, but it&#8217;s a good practice to do to avoid runtime exceptions being thrown.</li>
<li>A more subtle issue occurs when an api changes &#8211; for example if you wanted to refactor and add a new parameter, let&#8217;s say <code>userDetails</code> were versioned on the server and we wanted to be able to pull back a specific version, if no version is given we should pull back the latest.
<p>In the promises implementation we can add an extra param to the end and if it&#8217;s not set assume we want the latest, the signature looks like:</p>
<pre class="brush: jscript; title: ; notranslate">
        function getUserDetails(username, version) {…
</pre>
<p>We know that we don&#8217;t need to update any code when we add this because all our old code just passed in the username. However in the callback version in order to maintain backwards compatibility we&#8217;d need to add the param at the end:</p>
<pre class="brush: jscript; title: ; notranslate">
    function getUserDetails(username, successHandler, 
                            errorHandler, version) {
</pre>
<p>Hmm… this just feels wrong, we could solve this by creating a new implementation call <code>getUserDetailsVersioned</code> or refactor all existing calls to pass in <code>null</code> before our callbacks. In the end though it feels like we&#8217;re working around a bad pattern.</p>
</li>
</ol>
<p>One other key point is that if you want to chain your <code>then</code> functions then you just need to return promise&#8217;s from each of the functions. Therefore:</p>
<pre class="brush: jscript; title: ; notranslate">
    getUserDetails(&quot;matt&quot;)
           .then(getTweetsForUser)
           .then(addTweetsToUi, showUiErrorMessage);
</pre>
<p>needs <code>getUserDetails</code> and <code>getTweetsForUser</code> to return a promise. However I&#8217;d suggest returning a promise from <code>addTweetsToUi</code> too &#8211; who knows when you may need to add a notification hook to the after render of the tweets.</p>
<h1>Where do I get a Promises implementation from?</h1>
<p>There are several implementations available. I personally use rsvp.js: <a href="https://github.com/tildeio/rsvp.js" title="https://github.com/tildeio/rsvp.js">https://github.com/tildeio/rsvp.js</a>. However there are others out there.</p>
<ul>
<li><a href="https://github.com/kriskowal/q" title="Q">Q</a></li>
<li><a href="https://github.com/cujojs/when" title="when.js">when.js</a></li>
<li><a href="https://github.com/deftjs/DeftJS/wiki/Promises%20API" title="Deft.js">Deft.JS</a></li>
</ul>
<h1>What about testing? You said it was easier to test with</h1>
<p>Yes I did&#8230; But I&#8217;m going to save that for another blog post</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/promises-in-ext-js-and-sencha-touch/">Promises in Ext.js and Sencha Touch</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/promises-in-ext-js-and-sencha-touch/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">15749</post-id>	</item>
		<item>
		<title>JSON, JSR-303, Spring MVC and Ext.js</title>
		<link>https://www.mattgoldspink.co.uk/json-jsr-303-spring-mvc-and-ext-js/</link>
					<comments>https://www.mattgoldspink.co.uk/json-jsr-303-spring-mvc-and-ext-js/#respond</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Thu, 24 Jun 2010 23:46:40 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=1219</guid>

					<description><![CDATA[<p>So I thought I&#8217;d post this in case others hit it. If you want to Spring MVC and POST JSON from an Ext JsonWriter to the server and have it populate your bean model and also get validated by JSR-303 validator then you&#8217;ll need to do a little work because it doesn&#8217;t work out of ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/json-jsr-303-spring-mvc-and-ext-js/">JSON, JSR-303, Spring MVC and Ext.js</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>So I thought I&#8217;d post this in case others hit it. If you want to Spring MVC and POST JSON from an Ext JsonWriter to the server and have it populate your bean model and also get validated by JSR-303 validator then you&#8217;ll need to do a little work because it doesn&#8217;t work out of the box.</p>
<p>In the end I had to do some ugly classpath overriding to get it to work in the way I expected by writing my own modified version of Spring&#8217;s HandlerMethodInvoker. If you hit similar issues I suggest following this JIRA: <a href="http://jira.springframework.org/browse/SPR-7114">http://jira.springframework.org/browse/SPR-7114</a> and feel free to use the attached code on the JIRA. It&#8217;s been working great for me now.</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/json-jsr-303-spring-mvc-and-ext-js/">JSON, JSR-303, Spring MVC and Ext.js</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/json-jsr-303-spring-mvc-and-ext-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1219</post-id>	</item>
		<item>
		<title>Sencha touch, YQL and BBC essential mix</title>
		<link>https://www.mattgoldspink.co.uk/sencha-touch-yql-and-bbc-essential-mix/</link>
					<comments>https://www.mattgoldspink.co.uk/sencha-touch-yql-and-bbc-essential-mix/#comments</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Sat, 19 Jun 2010 15:48:38 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=1234</guid>

					<description><![CDATA[<p>So following on from earlier post this week about writing an Ext app which uses YQL to show the BBC radio1 essential mix tracklisting I decided to re-implement the same thing but using the new sencha touch library. Here it is: BBC Essential Mix Trackisting for Android, Ipad and Iphone I&#8217;ll post later this week ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/sencha-touch-yql-and-bbc-essential-mix/">Sencha touch, YQL and BBC essential mix</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>So following on from earlier post this week about writing an Ext app which uses YQL to show the BBC radio1 essential mix tracklisting I decided to re-implement the same thing but using the new sencha touch library. </p>
<p>Here it is: <a href="http://www.mattgoldspink.co.uk/touch.html">BBC Essential Mix Trackisting for Android, Ipad and Iphone</a></p>
<p>I&#8217;ll post later this week details about how it works. For now take a look at the source code and enjoy.</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/sencha-touch-yql-and-bbc-essential-mix/">Sencha touch, YQL and BBC essential mix</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/sencha-touch-yql-and-bbc-essential-mix/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1234</post-id>	</item>
		<item>
		<title>Playing with Ext.js, YQL and BBC radio1 Essential Mix</title>
		<link>https://www.mattgoldspink.co.uk/playing-with-ext-js-yql-and-bbc-radio1-essential-mix/</link>
					<comments>https://www.mattgoldspink.co.uk/playing-with-ext-js-yql-and-bbc-radio1-essential-mix/#comments</comments>
		
		<dc:creator><![CDATA[Matt Goldspink]]></dc:creator>
		<pubDate>Wed, 16 Jun 2010 01:01:14 +0000</pubDate>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[Blog]]></category>
		<guid isPermaLink="false">http://www.mattgoldspink.co.uk/?p=1210</guid>

					<description><![CDATA[<p>I&#8217;m a big fan of the radio1 essential mix and list to almost every week. I&#8217;m also getting more and more stuck into Ext.js at work. I also saw a great talk by Christian Heilmann of Yahoo at Full Frontal last and he demo&#8217;ed YQL. So I thought I&#8217;d give it a go and make ...</p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/playing-with-ext-js-yql-and-bbc-radio1-essential-mix/">Playing with Ext.js, YQL and BBC radio1 Essential Mix</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;m a big fan of the radio1 essential mix and list to almost every week. I&#8217;m also getting more and more stuck into Ext.js at work. I also saw a great talk by Christian Heilmann of Yahoo at Full Frontal last and he demo&#8217;ed YQL. So I thought I&#8217;d give it a go and make a quick Ext app which uses YQL and pulls in the data of the tracklists for the Radio 1 Essential Mixes&#8230;<br />
And here it is: <a href="http://www.mattgoldspink.co.uk/bbcradiolastfm.html">http://www.mattgoldspink.co.uk/bbcradiolastfm.html</a><br />
To give a brief summary of how I did it</p>
<ol>
<li><strong>Getting the tracklisting from the BBC</strong><br />This basically involved me picking out a rough top level element from an essential mix page of tracks (firebug is lord). This resulted in the below YQL query:
<pre class="brush: sql; title: ; notranslate">
select * from html where url=&quot;http&quot;+&quot;://ww&quot;+&quot;w.bbc.co.uk/programmes/b00sdb2z&quot; and xpath='//div[@class=&quot;title&quot; or @class=&quot;title with-image&quot;]'
</pre>
</li>
<li><strong>Now getting this into a simple Ext.js ListView</strong><br />It was obvious to me I needed a Ext.data.ScriptTagProxy to achieve this in order to get the JSON-P cross domain call. Unfortunately the hard bit is getting the JSON from the YQL call into the right format for my JsonStore in Ext. Thankfully Ext has all the necessary hooks to do this it looks something like:
<pre class="brush: jscript; title: ; notranslate">
var dataStore = new Ext.data.JsonStore(
	{
		proxy : new Ext.data.ScriptTagProxy(
			{
				url : 'http'+'://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fw'+'ww.bbc.co.uk%2Fprogrammes%2Fb00sdb2z%22%20and%0A%20%20%20%20%20%20xpath%3D\'%2F%2Fdiv%5B%40class%3D%22title%22%20or%20%40class%3D%22title%20with-image%22%5D\'&amp;format=json',
				callbackParam: 'callback',
				restful: true
			}
		),
		root: 'query.results.div',
		fields: [{
                    name : 'artist',
                    convert : Ext.ux.mattgoldspink.bbcessentialmix.getArtistFromJson
                },{
                    name : 'track',
                    convert : Ext.ux.mattgoldspink.bbcessentialmix.getTitleFromJson
                }],
		allowBlank: false
	}
);
</pre>
<p>Where the convert functions were:</p>
<pre class="brush: jscript; title: ; notranslate">
function js_traverse(o, keyName) {
	var type = typeof o;
	if (o.class &amp;&amp; o.class === keyName)
	{
		return o.content;
	}
	if (type == &quot;object&quot;) {
		for (var key in o) {
			var output = js_traverse(o[key], keyName);
			if (output) {
				return output;	
			}
		 }
	} else {
		return undefined;
	}
};

Ext.ux.mattgoldspink.bbcessentialmix.getArtistFromJson = function(v, rec) {
	var artist = js_traverse(rec, 'artist');
	if (artist) {
			return artist;
	} else {
		return 'Unknown';	
	}
};

Ext.ux.mattgoldspink.bbcessentialmix.getTitleFromJson = function(v, rec) {
	var artist = js_traverse(rec, 'track');
	if (artist) {
			return artist;
	} else {
		return 'Unknown';	
	}
};
</pre>
<p>The interesting part here is that the BBC very helpfully markup all their tags so all I had to do was traverse the messy JSON tree until I found a class which marked either the title or artist and then return it (or &#8216;Unknown&#8217; if it was there).<br />Next we plug this into our Ext.ListView:</p>
<pre class="brush: jscript; title: ; notranslate">
var listView = new Ext.list.ListView({
	id: 'listview',
	store: dataStore,
	multiSelect: true,
	emptyText: 'No tracks found',
	columnSort: false,
	loadingText: 'Updating tracklist',
	tpl: new Ext.XTemplate(
	'&lt;tpl for=&quot;rows&quot;&gt;',
		'&lt;/tpl&gt;&amp;lt;tpl exec=&quot;values.row = xindex;&quot;&gt;',
			'&lt;dl&gt;',
				'&lt;tpl for=&quot;parent.columns&quot;&gt;',
				'&lt;dt style=&quot;width:{&amp;#91;values.width*100&amp;#93;}%;text-align:{align};&quot;&gt;',
				'&lt;em unselectable=&quot;on&quot;&lt;tpl if=&quot;cls&quot;&gt;',
                                ' class=&quot;{cls}&lt;/em&gt;&lt;/dt&gt;&lt;/tpl&gt;&quot;&gt;',
					'{[values.tpl.apply(parent)]}',
				'&lt;div class=&quot;x-clear&quot;&gt;&lt;/div&gt;',
			'&lt;/dl&gt;',
		''
	),
	columns: [
	{
		header: 'Track #',
		width: 0.04,
		align: 'left',
		dataIndex: 'row'
	},{
		header: 'Artist',
		width: 0.48,
		align: 'left',
		dataIndex: 'artist'
	},{
		header: 'Track',
		width: 0.48,
		align: 'left',
		dataIndex: 'track'
	}]
});
</pre>
<p>This code is a bit more complicated than I expected because by default Ext.ListView does not with the Ext.grid.RowNumberer, so after a bit of google-fu I discovered a post showing how to achieve this using the tpl property.</li>
<li><strong>Getting all the possible Essential Mixes</strong><br />I decided I&#8217;d add a TreePanel so you can pick other essential mixes and view their tracks too. To do this I needed to jump back to firebug and YQL to get the available tracklists. In short my YQL query ended up being
<pre class="brush: sql; title: ; notranslate">select * from html where url=&quot;http://www.bbc.co.uk/programmes/b006wkfp/episodes/2009&quot; and xpath='//a[@class=&quot;url&quot;]'</pre>
</li>
<li><strong>Getting the TreePanel to pull in the data from YQL</strong><br /> Again this is something I thought was going to be tough, but after looking at a few blog posts and examples I realised it was possible. My final TreeLoader implementation looks like:
<pre class="brush: jscript; title: ; notranslate">
Ext.ux.mattgoldspink.bbcessentialmix.TreeLoader = function(){
	Ext.ux.mattgoldspink.bbcessentialmix.TreeLoader.superclass.constructor.call(this);
	this.proxy = new Ext.data.ScriptTagProxy({
		url : this.dataUrl,
		callbackParam: 'callback',
		restful: true
	});
};
Ext.extend(Ext.ux.mattgoldspink.bbcessentialmix.TreeLoader, Ext.tree.TreeLoader, {
	dataUrl: 'http:'+'//query.yahooapis.com/v1/public/yql?'
+'q=select%20*%20from%20html%20where%20url%3D'
+'%22http%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes'
+'%2Fb006wkfp%2Fepisodes%2F2010%22%20and%0A'
+'%20%20%20%20%20%20xpath%3D\'%2F%2Fa%5B'
+'%40class%3D%22url%22%5D\'&amp;format=json',
	requestData : function(node, cb){
		this.proxy.setApi(
                     Ext.data.Api.actions.read, 
                     'http'+'://query.yahooapis.com/v1/public/yql?'
+'q=select%20*%20from%20html%20where%20url'
+'%3D%22http%3A%2F%2Fww'
+'w.bbc.co.uk%2Fprogrammes%2F'
+'b006wkfp%2Fepisodes%2F' + node.attributes.nodeid 
+ '%22%20and%0A%20%20%20%20%20'
+'%20xpath%3D\'%2F%2Fa%5B%40class'
+'%3D%22url%22%5D\'&amp;format=json');
		this.proxy.request('read', null, {}, {
			readRecords : function(o){
				return o.query.results.a;
			}
		}, this.addNodes, this, {node:node, cb:cb});
	},
	addNodes : function(o, arg){
		var node = arg.node;
		for(var i = 0, len = o.length; i &lt; len; i++){
			var foo = {};
			foo.text = o&amp;#91;i&amp;#93;.span.content,
			foo.url =  o&amp;#91;i&amp;#93;.href.replace(&quot;/programmes/&quot;, '');
			foo.leaf = true;
			var n = this.createNode(foo);
			if(n){
				node.appendChild(n);
			}
		}
		arg.cb(this, node);
	}
});
&amp;#91;/sourcecode&amp;#93;
&lt;br /&gt;The key bits here are that before I request data for a node I change the YQL query to pick up the data for the year node that was clicked. Then once the results come back I pull out the results node from the JSON and in my addNodes function I set up leaf node.&lt;/li&gt;
	&lt;li&gt;&lt;strong&gt;The onclick callback to reload the ListView with new tracklists&lt;/strong&gt;&lt;br /&gt;When someone clicks one of these new tracklist nodes in the TreePanel we want to update the ListView. This was done by adding a listener to the TreePanel: 

listeners: {
	click: function(n) {
		Ext.getCmp('listview').getStore().proxy.setApi(
                     Ext.data.Api.actions.read, 
'http://query.yahooapis.com/v1/public/yql?'
+'q=select%20*%20from%20html%20where'
+'%20url%3D%22http%3A%2F%2Fww'
+'w.bbc.co.uk%2Fprogrammes%2F' + n.attributes.url 
+'%22%20and%0A%20%20%20%20%20%20'
+'xpath%3D\'%2F%2Fdiv%5B%40class%3D'
+'%22title%22%20or%20%40class%3D'
+'%22title%20with-image%22%5D\'&amp;format=json');
		Ext.getCmp('listview').getStore().load();
	}
}
</pre>
<p>We simply change the read action url in the ListView store&#8217;s proxy, then we reload the store! As simple as that.</li>
</ol>
<p>To wrap it up I put it all in an Ext.Viewport. Feel free to view the source of the page and enjoy!<br />
<a href="http://www.mattgoldspink.co.uk/bbcradiolastfm.html">http://www.mattgoldspink.co.uk/bbcradiolastfm.html</a></p>
<p>The post <a rel="nofollow" href="https://www.mattgoldspink.co.uk/playing-with-ext-js-yql-and-bbc-radio1-essential-mix/">Playing with Ext.js, YQL and BBC radio1 Essential Mix</a> appeared first on <a rel="nofollow" href="https://www.mattgoldspink.co.uk">Matt Goldspink</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mattgoldspink.co.uk/playing-with-ext-js-yql-and-bbc-radio1-essential-mix/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1210</post-id>	</item>
	</channel>
</rss>
