Hacking the core: ordering your application components
Monday, November 23, 2009 12:33:21 AM
Yusef is a nice toolkit, but there are times where it simply won't cut it. One of the things I dislike about yusef is its promise to load every script in the serverScripts directory. Sure, I want each of my script of my app loaded. But I also need some parts loaded before some others (e.g. some utility functions before fetching data off the disk).
Because yusef is all javascript, hacking one's way through it is fairly easy. So let's force yusef to load your application component using the total order imposed by the file name.
Finding yusef's serverScripts loading procedure
One question I asked myself when I first started using yusef was "How does it know that my application is in serverScripts?". It turns out that it knows the directory because of some magic constant set in its configuration:
Searching for this constant in the core will show the _loadServerScripts procedure up easily. This is indeed the one procedure we want to hack.
Ordering the files
The _loadServerScripts procedure fetches the script files in the serverScripts {File} variable. This variable is then iterated to load each script in turn.
From this point, ordering the files is a breeze. For my use, simply ordering them by name is enough, but smart coders out there may prefer to order them using some hyper sophisticated comparator. So here's the hack:
All that's left is to properly name files. For this task, I tend to use a two digits code which goes as follows:
With x chosen to resolve other dependencies that may occurs, or to add some layering semantic.
Extending the system
Sometimes, extending your application with our hack is not enough. Some functionalities that are used over and over, are better fit for a library. It turns out that yusef provides a libraries loader that can be used for that purpose. On a side note, this libraries loader takes care of dependencies between libraries; therefore the naming hack won't be necessary here.
One library yusef does not provide you with is uuid generation. Adding such a library can be done as follows:
Setup.xml files describes how your library is to be loaded. It takes the following format:
When parsing this file, the libraries loader will first resolve all libraries specified in depend. Then all script files will be loaded.
Extending yusef is cool!
Because yusef is all javascript, hacking one's way through it is fairly easy. So let's force yusef to load your application component using the total order imposed by the file name.
Finding yusef's serverScripts loading procedure
One question I asked myself when I first started using yusef was "How does it know that my application is in serverScripts?". It turns out that it knows the directory because of some magic constant set in its configuration:
_config.serverScriptsPath = '/serverScripts/';
Searching for this constant in the core will show the _loadServerScripts procedure up easily. This is indeed the one procedure we want to hack.
Ordering the files
The _loadServerScripts procedure fetches the script files in the serverScripts {File} variable. This variable is then iterated to load each script in turn.
From this point, ordering the files is a breeze. For my use, simply ordering them by name is enough, but smart coders out there may prefer to order them using some hyper sophisticated comparator. So here's the hack:
serverScripts.refresh();
serverScripts.sort(); /* Added: sort scripts by name */
for( var i=0,currentFile; currentFile=serverScripts[i]; i++ )
All that's left is to properly name files. For this task, I tend to use a two digits code which goes as follows:
- 0x: utils and or models
- 1x: controllers or views
-
2x: Sorry, i'm very binary, i can't count up to 2
With x chosen to resolve other dependencies that may occurs, or to add some layering semantic.
Extending the system
Sometimes, extending your application with our hack is not enough. Some functionalities that are used over and over, are better fit for a library. It turns out that yusef provides a libraries loader that can be used for that purpose. On a side note, this libraries loader takes care of dependencies between libraries; therefore the naming hack won't be necessary here.
One library yusef does not provide you with is uuid generation. Adding such a library can be done as follows:
- Get some nice uuid script or write one yourself.
- Create a uuid subdirectory of the libraries folder and put your uuid script. We suppose in the following that the script is named uuid.js.
- Add a new file in your uuid subdirectory named setup.xml containing the following:
<setup id="uuid"> <script>uuid.js</script> </setup>
Setup.xml files describes how your library is to be loaded. It takes the following format:
<setup id="your-library-id">
<script>script_1.js</script>
...
<script>script_n.js</script>
<depend>library_1</depend>
...
<depend>library_n</depend>
</setup>
When parsing this file, the libraries loader will first resolve all libraries specified in depend. Then all script files will be loaded.
Extending yusef is cool!





