Ext.data.JsonP.command_app_multi({"title":"Multi-Page Ext JS Apps","guide":"
Contents
\n\nWorkspaces in Sencha Cmd describes the new workspace support\nin Sencha Cmd that is designed specifically to facilitate large, multi-page applications.
\n\nThis guide picks up where that guide left off and describes how to use lower-level\ncommands to perform some advanced build operations. These are primarily focused on creating\ntwo scripts per page instead of the typical one (\"all-classes.js\"
) to improve caching as\nusers navigate between pages. While there are many variations on the ideas discussed here\nthat could be similarly implemented, this guide describes two approaches:
\"common.js\"
file.\"common.js\"
file.Further, since we are using lower-level commands in this guide, we use a custom application\nfolder structure to show how Sencha Cmd can be used to fit your own specific choices for\ncode organization.
\n\nThis guide focuses primarily on Ext JS applications. Support for these techniques will be\navailable for Sencha Touch in future releases.
\n\nTo consider applications that do not have a workspace, lets assume that we have a two-page\napplication with the following folder structure:
\n\nbuild/ # The folder where build output is placed.\ncommon/ # Things common to all pages of the application.\n src/ # Shared JavaScript code for all pages.\next/ # The framework distribution.\n src/ # The framework source tree.\npage1/\n index.php # The markup file for page 1.\n src/ # Folder containing JavaScript code unique to page 1.\npage2/\n index.php # The markup file for page 2.\n src/ # Folder containing JavaScript code unique to page 2.\n
\n\nThis example could be extended to cover many more pages, which would make it harder to\nfollow the example commands. There are some features that only apply to applications with\nthree or more pages so we will expand the example to illustrate that usage.
\n\nIf users of the application tend to visit more than one page, it may be helpful to split\nup the code such that common code is in a shared file while page-specific code is isolated\nto a second script file.
\n\nIn set operations terminology this is called a set \"intersection\". That is to say, we want to\ntake the files in the intersection of the two sets of files needed by each page and generate\na file with just those classes.
\n\nThe following command will do precisely that:
\n\nsencha compile -classpath=ext/src,common/src,page1/src,page2/src \\\n page -name=page1 -in page1/index.php -out build/page1/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page2/index.php -out build/page2/index.php \\\n -scripts ../common.js and \\\n intersect -set page1,page2 and \\\n save common and \\\n concat -yui build/common.js and \\\n restore page1 and \\\n exclude -set common and \\\n concat -yui build/page1/all-classes.js and \\\n restore page2 and \\\n exclude -set common and \\\n concat -yui build/page2/all-classes.js\n
\n\nLet's look closely at what each part of this command accomplishes.
\n\nThe first thing is to create the compile
context and tell it the classpath
for all of\nthe source code folders:
sencha compile -classpath=ext/src,common/src,page1/src,page2/src \\\n
\n\nThen we use two page
commands to include the source from each page as well as generate\nthe appropriate output pages in the build
folder. Each page
command produces a set\nof files containing exactly the files needed by that page. These sets are given the names\npage1
and page2
. Finally, each generated output page will get an extra script
tag\nwhose src
attribute is \"../common.js\"
.
page -name=page1 -in page1/index.php -out build/page1/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page2/index.php -out build/page2/index.php \\\n -scripts ../common.js and \\\n
\n\nNow that all of the files needed by each page are recorded in two sets, we use intersect
\nto determine the files needed by both pages. Only these files will be included in the\ncurrent set.
intersect -set page1,page2 and \\\n
\n\nWe use save
to record the current set of files (the result of the intersection). These\nare the files we will put in \"common.js\"
. The name for the new set is common
.
save common and \\\n
\n\nThen we use concat
to combine the files and produce \"build/common.js\"
(also compressing\nthe file using `-yui' to engage the YUI Compressor).
concat -yui build/common.js and \\\n
\n\nNow we need to produce the \"all-classes.js\"
for each page, so we use restore
to make\nthe current set equal to the previously saved set for the page:
restore page1 and \\\n
\n\nThen we remove from this set all of the files that we just generated in \"common.js\"
:
exclude -set common and \\\n
\n\nAnd then use concat
again to produce \"all-classes.js\"
for the page:
concat -yui build/page1/all-classes.js and \\\n
\n\nWe repeat the last few steps again for page2
:
restore page2 and \\\n exclude -set common and \\\n concat -yui build/page2/all-classes.js\n
\n\nA different way to partition shared code would be to isolate all of the framework code\nneeded by the application and produce a file similar to \"ext-all.js\"
but only containing\nthose classes needed by some part of the application. This approach might load more of the\nframework than needed by each page, but the benefits of the browser cache could easily\nmake up for this increase.
The following command contains only a slight adjustment to the above:
\n\nsencha compile -classpath=ext/src,common/src,page1/src,page2/src \\\n page -name=page1 -in page1/index.php -out build/page1/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page2/index.php -out build/page2/index.php \\\n -scripts ../common.js and \\\n union -set page1,page2 and \\\n exclude -not -namespace Ext and \\\n save common and \\\n concat -yui build/common.js and \\\n restore page1 and \\\n exclude -set common and \\\n concat -yui build/page1/all-classes.js and \\\n restore page2 and \\\n exclude -set common and \\\n concat -yui build/page2/all-classes.js\n
\n\nThe difference between this command and the previous command is in how the common
set is\ncalculated.
union -set page1,page2 and \\\n exclude -not -namespace Ext and \\\n
\n\nIn this case the union
command is used to include all files used by either page. This\nset is then reduced using the exclude
command to remove all classes that are not in the\nExt namespace. This will leave only the framework code that is needed by either page in\nthe current set.
The remainder of the command above and below these two lines is the same as before.
\n\nApplications with more than two pages can be managed as an extension of a two-page\napplication as discussed above. Just add extra page
commands (one for each page) and\nextra set operations to produce the appropriate \"all-classes.js\"
file for each page.
There are interesting possibilities for code sharing among the multiple pages. For\nexample, let's consider a five-page application structured in the same basic way.
\n\nIt may be that the common set of files produced by the intersection of all pages is quite\nsmall. This will force code that is not used by all pages out of \"common.js\"
and into an\n\"all-classes.js\"
file for each page. One strategy for dealing with this is to manually\ndivide up similar pages and treat the application as multiple, independent, multipage\napplications.
Another, simpler, way would be to use a \"fuzzy intersection,\" that is an operation the\nselects all classes used by a specified minimum number of pages. Here's an example:
\n\nsencha compile -classpath=ext/src,common/src,page1/src,page2/src \\\n page -name=page1 -in page1/index.php -out build/page1/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page2/index.php -out build/page2/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page3/index.php -out build/page3/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page4/index.php -out build/page4/index.php \\\n -scripts ../common.js and \\\n page -name=page2 -in page5/index.php -out build/page5/index.php \\\n -scripts ../common.js and \\\n intersect -min=3 -set page1,page2,page3,page4,page5 and \\\n save common and \\\n concat -yui build/common.js and \\\n restore page1 and \\\n exclude -set common and \\\n concat -yui build/page1/all-classes.js and \\\n restore page2 and \\\n exclude -set common and \\\n concat -yui build/page2/all-classes.js and \\\n restore page3 and \\\n exclude -set common and \\\n concat -yui build/page3/all-classes.js and \\\n restore page4 and \\\n exclude -set common and \\\n concat -yui build/page4/all-classes.js and \\\n restore page5 and \\\n exclude -set common and \\\n concat -yui build/page5/all-classes.js\n
\n\nOther than the three additional page
commands as well as three stanzas of restore
,\nexclude
and concat
, the above command only changed from the original intersection in\nthis one way:
intersect -min=3 -set page1,page2,page3,page4,page5 and \\\n
\n\nThe -min
switch activated the fuzzy intersection method. By default, intersect
selects\nclasses used by 100% of the specified sets or, in this case, all 5 sets. With -min
you\ncan override this threshold. By specifying -min=3
we are saying to include in the current\nset any class used by at least 3 sets (or 60%).