jit - Just in Time Code
Status: Beta
Brought to you by:
fetmata
File | Date | Author | Commit |
---|---|---|---|
tests | 2009-09-14 | fetmata | [r12] |
jit.js | 2010-06-16 | fetmata | [r13] |
readme.txt | 2010-06-16 | fetmata | [r13] |
jit - Just in Time by Martin Gustavsson (inspired by Jan Jarfalk's jQuery.lazy) jit enables lazy loading of script resources by injecting proxy functions that will load the required script the first time they are used. In contrast to Jan's script this one works without jQuery, but the concept is much the same. Changelog 0.5 (2010-xx-xx) - Script sources will no longer be prefixed with jit.dir if they are absolute. - Added the possibility to instantly load a script without using a proxy. 0.4 (2009-09-14) - The default value of jit.dir is now the dir in which jit.js is located. I found this to be much more useful than the old "same as page" default of "". 0.3 (2009-07-30) - Changed the examples into tests. - Modified the func function to be less brutal on existing hierarchies. 0.2 (2009-07-23) - Added examples and this readme. - Added support for square brackets in proxy paths. - Fixed evaluation of loaded script into global scope for MSIE, Opera and Safari. - Fixed the code that checks if the proxy is used as a constructor. 0.1 (2009-07-22) - Initial version Reference jit(src) The root function. Takes a script source and returns its jit object. The script must be located on the same domain as your page. This is due to a cross domain security policy for XMLHttpRequest implemented by most browsers (to date). Unfortunately, XMLHttpRequest is the only way I know of to request a script synchronously, which is vital to jit. jit object methods Most of these methods return the object itself, so they can be used in any order and as many times as you like. cache(val) Set browser caching true or false. False will append a random seed to any url. Overrides the global setting jit.cache which defaults to "true". css(arguments) Adds each string argument as a css resource to be loaded together with the script. dir(val) Sets the directory the script and css resources are relative to. Overrides the global setting jit.dir which defaults to the dir jit.js is located in. load Force loading of both script and css resources instantly without the need to invoke any of the proxies. Does not return the jit object since additional options appended after this point will have no effect. proxy(arguments) Creates a proxy to the script at each string argument passed. Will NOT overwrite existing functions. Examples jit("foobar.js").proxy("foo","bar").css("foobar.css").dir("../resources/").cache(false); ...which is the same as... jit("../resources/foobar.js").cache(false).proxy("foo").css("../rosources/foobar.css").proxy("bar"); ...which is the same as... jit.dir = "../resources/"; jit.cache = false; jit("foobar.js").proxy("foo","bar").css("foobar.css"); Enough of that. What happens next? Well. You simply use foo() or bar() in the code. And the first time either one of them is called both foobar.js and foobar.css will be loaded. Hopefully foobar.js contains code declaring these two functions, effectively overwriting the proxies. Should foobar.js fail to overwrite either function, that function will be replaced with one throwing an exception (to avoid endless recursion).