Welcome to part 2 of the Ext JS from Scratch blog series. In the part 1 blog post, we built up to a working application from scratch that focused on what we want our application to look like and what we want it to do; not HOW it is built from an HTML and CSS point of view.
As a reminder, we ended up with the application that you can see here:
In that application, you might have noticed that we ‘hard coded’ the needed files in index.html, specifically the .js and .css files on lines 5 – 7:
Ext JS From Scratch – Part 1, Demo 4
In an application that you build, those files will constantly be added to and will change over time. Files will be added with new functionality, for example, as we add new views. Also, files will change as we go from development to production, and from production to a testing build – so having some way to manage those files is a must.
With Ext JS, file management is achieved with a combination of something we call the micro loader and Sencha Cmd. A typical index.html file from a Sencha Cmd generated application does not include the files you need in index.html, but instead downloads them using the micro loader. A typical index.html file generated with Sencha Cmd looks like this:
But the micro loader has always been a mystery to most – what does it do? What files does it generate? When do I need to regenerate files? What regenerates files? Well, in typical Ext JS from Scratch fashion, lets build our own version of the micro loader!
As I mentioned, Ext JS applications that you generate with Sencha Cmd include only 1 file in the index.html – the micro loader. Here is an example:
This single file – bootstrap.js – is the bootstrap loader file, or the micro loader file. Note that this file has NOTHING to do with the bootstrap library that helps with html layout. I will show you in a future post how we can change the name of that file to avoid confusion, maybe to something more appropriate like microloader.js. For this post, we will use the name ‘bootsimple.js’.
It is also worth noting the micro loader does many things, which we will cover in future posts. For now we will focus on the file loading feature.
The micro loader works by reading the needed files from a manifest file – by default this file is bootstrap.json. The bootstrap.json file is populated by Sencha Cmd as you do things like add files to your project, change the theme, etc.
At a high level, here is what the micro loader does to load the necessary files for your application:
- Define the global Ext variable
- Read the ‘manifest’ file (bootstrap.json)
- Wait for manifest file read to complete
- Produce DOM elements for .js and .css files
- Add .css DOM elements to head tag
- Add first .js DOM element to head tag (Ext framework file)
- Wait for framework .js file to completely load
- Add remaining .js DOM elements to head tag
- When all files are loaded, call the the launch event in app.js
So let’s take a look at a simplified version of the micro loader. As I mentioned, I named this file ‘bootsimple.js’, and have included it in the index.html file. Each step above produces a console.log, so you can follow the logic of the simple version of the micro loader for yourself here: https://github.com/mgusmano/ExtJSFromScratch/tree/master/Part02/02
With Ext JS, we manage the loading of files into the browser with a command line tool called Sencha Cmd. This is a free tool and, as you will see in this series, Sencha Cmd not only helps you scaffold and bootstrap an initial application, but is used in the entire lifecycle of your application development process with Ext JS. If you are not using Sencha Cmd, you are missing out on a lot of our best practices in regards to project structure performance and many other things.
Sencha Cmd is available to download here: https://staging.sencha.com/products/extjs/cmd-download/
Now we are ready to create our application with Sencha Cmd. To scaffold an initial application, run the following in a terminal window. You will need to replace with the location where you installed the Ext JS framework.
sencha -sdk generate app -modern -r -starter=false FromScratch ./03
When you take a look at the generated application folder, you will now see some familiar files: bootstrap.js and bootstrap.json (and bootstrap.jsonp, which is used for some cross domain situations). There are also several files produced by Sencha Cmd which we will discuss in future posts; workspace.json, build.xml, bootstrap.css, app.json, and the ext and app folders.
We have shown just a small part of what the micro loader does, including its ability to dynamically load files from a manifest file created by Sencha Cmd. In the next post, we will more fully look at Sencha Cmd and how you use it during your normal development efforts. Here are some of the other benefits of using the micro loader and Sencha Cmd when building your Ext JS application:
- Caching of files
- Disabling or enabling of caching
- Synchronous and asynchronous loading
- Normalizing of URL’s (creating canonical URL’s)
- Dealing with cross domain issues
- Setting platform tags
- Calling Ext.BeforeLoad for Ext JS 6 Universal applications
- Handling various IE issues
Sencha Cmd also has templates you can use to create your own starter application. We will look at that as well in the next post – stay tuned!
New to Ext JS? Join our Upcoming Webinar
If you are new to Ext JS or just getting started, we invite you to join our upcoming webinar, A Day in the Life of an Ext JS Developer, taking place February 15th at 10am PST. Join us and you will learn the benefits of using Ext JS as well as best practices for getting started. Register here.
There shouldn’t be body tag in sencha CMD example.
Marc Gusmano says
correct Tomas, I will see if I can get that removed – thanks for pointing that out
This series is a great idea –please speed up the publication pace :-)
Marc Gusmano says
Thanks for the feedback – I am already working on part 3…..
looking forward to part 3, Do you have any guess when can we expect it?
Simple and straight forward blog post that helps ease the introductory learning curve for ExtJS.
Suggestion: Please allow for any images/screenshots to be opened when clicked so we can see the original size of the image for better viewing.
Marc Gusmano says
James, thanks for the feedback – I will see if we can do that for the next post
Steve Mercer says
Looking at 02/bootsimple.js in the repository it references a file named bootstrap.json but that file is not part of the repository. What am I missing here?
I’m experiencing the same error, did you find the way to solve this problem?
Fixed ! It is the code in one of the screenshot above
I have error on: sencha -sdk generate app -modern -r -starter=false FromScratch ./03
Sencha Cmd v18.104.22.168
[ERR] The specified string cannot be converted into a valid namespace identifier
[ERR] Invalid namespace : FromScratch
uname – a
FreeBSD cup.hochuvotpusk.ru 11.1-RELEASE-p8 FreeBSD 11.1-RELEASE-p8 #0: Tue Mar 13 17:07:05 UTC 2018 email@example.com:/usr/obj/usr/src/sys/GENERIC amd64
mingyu ren says
Thanks for sharing!
The series is very suitable for my need, I especially want to know about the micro loader, looking forward to part 3