├── MIT-LICENSE.txt ├── README.md ├── demos ├── feature-tests │ ├── advanced │ │ ├── 1_named-deferreds-no-globals.html │ │ ├── 2_no-cache.html │ │ ├── 3_aliases-for-definitions.html │ │ ├── 4_multiple.html │ │ ├── 5_overlapping-composites.html │ │ └── async-templates │ │ │ ├── 1_pass-deferred.html │ │ │ ├── 2_pass-url.html │ │ │ ├── 3_wait-also-for-domready.html │ │ │ ├── 4_remote-composite-templates.html │ │ │ └── jquery.tmpl.experimental.js │ ├── basic │ │ ├── 1a_delay-domready.html │ │ ├── 1b_delay-domready.html │ │ ├── 2a_def.html │ │ ├── 2b_def.html │ │ ├── 3_join-with-when.html │ │ ├── 4_bare.html │ │ ├── 5a_composite.html │ │ ├── 5b_composite-with-def.html │ │ ├── 6_static.html │ │ ├── 7_fail.html │ │ ├── 8_add-dependencies.html │ │ └── 9_template-and-data.html │ ├── console-test.js │ ├── resources │ │ ├── bare1.js │ │ ├── bare1.min.js │ │ ├── bare2.js │ │ ├── f.js │ │ ├── folder1 │ │ │ ├── a.js │ │ │ ├── a.min.js │ │ │ └── add-dependencies.js │ │ ├── folder2 │ │ │ ├── c.js │ │ │ ├── composite-dcf.js │ │ │ ├── composite-fed.js │ │ │ ├── d.js │ │ │ └── e.js │ │ └── myscriptdefs.js │ ├── templates-and-data │ │ ├── mydata.js │ │ ├── myrichdata.js │ │ ├── mytmpl.js │ │ └── richtmpls.js │ └── with-or-without-jquery │ │ ├── 1_with-jquery.html │ │ └── 2_non-jquery-version.html └── movies │ ├── pages │ ├── 1_movies.html │ ├── 2_movies-when-data-and-script.html │ ├── 3_movie-lazy-load.html │ └── 4_movies-and-jquery-lazy-load.html │ └── resources │ ├── css │ ├── jquery-ui-1.8.1.custom.css │ └── movies.css │ ├── images │ ├── ui-bg_diagonals-thick_75_f3d8d8_40x40.png │ ├── ui-bg_dots-small_65_a6a6a6_2x2.png │ ├── ui-bg_flat_0_333333_40x100.png │ ├── ui-bg_flat_65_ffffff_40x100.png │ ├── ui-bg_flat_75_ffffff_40x100.png │ ├── ui-bg_glass_55_fbf8ee_1x400.png │ ├── ui-bg_glass_75_dadada_1x400.png │ ├── ui-bg_highlight-hard_100_eeeeee_1x100.png │ ├── ui-bg_highlight-hard_100_f6f6f6_1x100.png │ ├── ui-bg_highlight-soft_15_cc0000_1x100.png │ ├── ui-icons_004276_256x240.png │ ├── ui-icons_cc0000_256x240.png │ └── ui-icons_ffffff_256x240.png │ ├── jqueryplugins │ └── templates │ │ ├── jquery.tmpl.js │ │ └── jquery.tmplplus.js │ ├── jqueryui │ ├── datepicker │ │ ├── datepicker.js │ │ └── datepicker.min.js │ ├── jqueryuidefs.js │ └── pager │ │ └── jquery.pager.js │ ├── movieapp.js │ ├── movieappdefs.js │ └── movieplugin.js ├── jquery.defer.js ├── jquery.defer.min.js ├── jquery.defer.min.js.map ├── jsdefer.js ├── jsdefer.min.js └── jsdefer.min.js.map /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Boris Moore https://github.com/BorisMoore/jsdefer 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JsDefer recent Status: 2 | 3 | There have been some breaking changes in jQuery APIs for Deferred since JsDefer was implemented. 4 | 5 | A recent JsDefer commit modifies jquery.defer.js to make it agains compatible with current 6 | jQuery APIs. 7 | 8 | However the non-jquery version of JsDefer - jsdefer.js - uses the previous Deferred APIs, 9 | which means that you cannot currently load jsdefer.js and then have it load jQuery! 10 | You need to determine up front whether you want the version with or without jQuery... 11 | 12 | Apart from the above fixes, JsDefer is not being actively updated at the moment, but will 13 | probably become active again at some point after JsViews V1.0 has shipped. 14 | 15 | # JsDefer Features: 16 | 17 | * Wrapped scripts 18 | * Unwrapped scripts 19 | * Optional script definition (deferDef) defines dependencies 20 | * Optional deferring of DomReady at global or individual request level 21 | * Can also add dependencies for script - or add new definitions - in the script itself, if wrapped 22 | * Execution order based on dependency 23 | * Parallel/serial loading based on definitions, dependency, and wrapped versus unwrapped, and whether 24 | dependencies are defined in separate def file or in script itself. 25 | * Passing options to scripts 26 | * Wrapped script can provide a return value 27 | * Failure bubbling 28 | * Integration with Deferred (as in jQuery 1.5) enables or simplifies many use-case scenarios 29 | * Use of $,when and other deferred features: `when( scripts, json, domReady ).then( myCallback );` 30 | * Exposing 'promises' (named deferreds) 31 | * Setting: min true/false, for debug versus minified versions 32 | * Wrapped scripts 'self executing' when used as static scripts 33 | * Composite scripts (also able to be loaded statically in page) 34 | * jQuery independent version 35 | * Small 36 | 37 | ## Demos 38 | 39 | ### Complete Demos 40 | * Different versions of the [Movies Demo app](https://github.com/BorisMoore/jsdefer/tree/master/demos/movies/pages) 41 | are provided to illustrate different use scenarios for JsDefer. 42 | 43 | ### Feature tests: 44 | 45 | * Basic Features: Currently no unit tests are provided (they will come) but the 46 | [FeatureTests/Basic](https://github.com/BorisMoore/jsdefer/tree/master/demos/feature-tests/basic) 47 | folder provides some examples for testing different feature details. 48 | * Advanced Features: 49 | [This folder](https://github.com/BorisMoore/jsdefer/tree/master/demos/featuretests/with-or-without-jquery) 50 | shows how to use JsDefer with or without jQuery. 51 | [These samples](https://github.com/BorisMoore/jsdefer/tree/master/demos/feature-tests/advanced) 52 | illustrate some less-commonly used features. 53 | The [AsyncTemplates](https://github.com/BorisMoore/jsdefer/tree/master/demos/feature-tests/advanced/async-templates) 54 | folder explores some experimental integration with 55 | [jQuery Templates](https://github.com/BorisMoore/jquery-tmpl). 56 | 57 | 58 | ## Syntax Examples: 59 | 60 | 61 | ### Request deferred script loading 62 | 63 | // Note: If scripts have been defined in a deferDef declaration, this will 64 | // trigger parallel loading of all dependent scripts, 65 | // and will execute scripts in the correct order 66 | 67 | // Load movies script 68 | $.defer( "...movieApp.js" ) 69 | .done( workWithMovies ) 70 | .fail( failCallback ); 71 | 72 | 73 | ### Deferred script loading using delayed DomReady event 74 | 75 | $.deferSettings.delayDomReady = true; 76 | 77 | $.defer( "...movieApp.js" ); 78 | 79 | $( function() { 80 | workWithMoviesAndDom(); 81 | }); 82 | 83 | 84 | ### Passing options and using return value 85 | 86 | $.defer( "...movieApp.js", { pageSize: 4 }) 87 | .done( function( movieApp ) { 88 | workWithMovies( movieApp ); 89 | }); 90 | 91 | 92 | ### Using $.when to handle parallel async processes 93 | 94 | // Load both data and scripts in parallel, 95 | // and process when data, scripts and DOM are ready 96 | $.when( 97 | $.defer( "...movieApp.js", { pageSize: 4 } ), 98 | getMovies( "Cartoons" ), 99 | $.ready 100 | ) 101 | .done( function( movieApp, data ) { 102 | workWithMoviesAndDom( movieApp ); 103 | }); 104 | 105 | 106 | ### Setting default options 107 | 108 | // Use unminified versions of script 109 | $.deferSettings.min = false; 110 | 111 | // Delay DomReady by default. (Can also override as a setting, for individual defer() requests.) 112 | $.deferSettings.delayDomReady = true; 113 | 114 | 115 | ### Create deferDef definition - to load and execute dependencies in correct order 116 | 117 | $.deferDef({ 118 | // Just set the URL 119 | tmpl: "http://...jquery.tmpl.js", 120 | 121 | // Specify url and dependencies 122 | tmplplus: { 123 | url: "http://.../jquery.tmplplus.js", 124 | depends: "tmpl" 125 | }, 126 | 127 | tmplCombined: { 128 | url: "myCombinedFiles/tmplCombined.js", 129 | 130 | // This script has both minified and unminified versions 131 | urlMin: "myCombinedFiles/tmpl.min.js", 132 | 133 | // It is a combined script: it can be used in place of the tmpl and tmplplus scripts 134 | contains: [ "tmpl", "tmplplus" ] 135 | }, 136 | 137 | yahooHelper: { 138 | url: "http://.../yahooHelper.js", 139 | 140 | // This script is not wrapped, and therefore will load sequentially after its dependencies: 141 | // i.e. it will only be requested after any script that it depends on has loaded and executed 142 | bare: true, 143 | }, 144 | 145 | movieApp: { 146 | url: "movieApp.js", 147 | 148 | // Note that this script is wrapped (default is bare: false), and so will be loaded in parallel along 149 | // with its dependencies. However its contents will not be executed until after execution of dependent scripts. 150 | 151 | // Depends on both a declared and undeclared scripts 152 | depends: [ "tmplplus", "http://...datamodel.js", "yahooHelper" ] 153 | } 154 | 155 | }); 156 | 157 | // Can optionally use typed methods to do a deferred load of any resource script defined in 158 | // the deferDef definition (plus its dependencies): 159 | 160 | $.defer.movieApp() 161 | .done( workWithMovies ) 162 | .fail( failCb ); 163 | 164 | 165 | ### Wrapped script 166 | 167 | _Note:_ This can be loaded by any script loader that recognizes 168 | the $deferRun global name for the wrapper function. 169 | 170 | $deferRun( 171 | 172 | function( $, options ) { 173 | 174 | // Script code here 175 | doStuff( options ); 176 | return myObject; 177 | 178 | } 179 | 180 | ); 181 | 182 | 183 | ### Self-executing wrapper 184 | 185 | _Use this wrapper syntax to create a wrapped script which can also be loaded as a static script 186 | in the absence of a script loader recognizing the $deferRun wrapper function_ 187 | 188 | ((window.$deferRun || function( run ){ run(); }) ( 189 | 190 | function( $, options ) { 191 | 192 | // Script code here 193 | 194 | } 195 | 196 | )); 197 | 198 | 199 | ### Declare dependencies on script itself 200 | 201 | _Note:_ If the dependent script was already declared in a deferDef definition, then 202 | it will have loaded in parallel. Otherwise, if only declared here, it will be loaded 203 | in series - after this script loads, but before the body of this script is executed. 204 | 205 | ((window.$deferRun || function( run ){ run(); }) ( 206 | 207 | function( $, options ) { 208 | 209 | // Script code here 210 | 211 | }, 212 | 213 | // Declare one or more dependent scripts 214 | "myOtherCode.js" 215 | 216 | )); 217 | 218 | 219 | ### Declare dependencies and a deferDef definition, on the script itself 220 | 221 | ((window.$deferRun || function( run ){ run(); }) ( 222 | 223 | function( $, options ) { 224 | 225 | // Script code here 226 | 227 | }, 228 | { 229 | // Declare one or more dependent scripts 230 | depends: ["...myOtherCode.js", "myComponent"], 231 | 232 | // Declare some deferDer script definitions 233 | def: { 234 | myComponent: { 235 | url: "...myComponent.js", 236 | minUrl: "...myComponent.min.js", 237 | depends: "...componentCore.js" 238 | } 239 | } 240 | })); 241 | 242 | 243 | ### Script combination: Composite scripts 244 | 245 | The different wrapped scripts within this script are identical to the 246 | individual wrapped scripts that they replace. 247 | 248 | _tmplCombined.js:_ 249 | 250 | $deferRun( 251 | function( $, options ) { 252 | 253 | $deferRun( 254 | function( $, options ) { 255 | // Script code for tmpl.js here 256 | }); 257 | 258 | $deferRun( 259 | function( $, options ) { 260 | // Script code for tmplplus here 261 | }); 262 | 263 | }); 264 | 265 | _Associated script definition, and invocation_ 266 | 267 | $.deferDef({ 268 | tmplCombined: { 269 | url: "myCombinedFiles/tmplCombined.js", 270 | 271 | contains: [ "http://...tmpl.js", "http://...tmplplus.js" ] 272 | } 273 | }); 274 | 275 | $.defer.tmplCombined(); 276 | 277 | // Note: The above will make one HTTP request for the composite file, 278 | // but is otherwise equivalent to the following two requests: 279 | 280 | //$.defer( "http://...tmpl.js" ); 281 | //$.defer( "http://...tmplplus.js" ); 282 | 283 | // The individual wrapped scripts will execute in the correct order based on the 284 | // declared dependencies of the individual scripts files they represent, 285 | // but the individual files will not be loaded, once the composite 286 | // script has been requested. 287 | 288 | 289 | ### Can also declare deferDef script definitions, dependencies, etc. on the composite file itself 290 | 291 | $deferRun( 292 | 293 | function( $, options ) { 294 | 295 | $deferRun( 296 | function( $, options ) { 297 | // Script code for sub script 1 here 298 | }); 299 | 300 | $deferRun( 301 | function( $, options ) { 302 | // Script code for sub script 2 here 303 | }); 304 | 305 | }, 306 | { 307 | depends: [ "...OtherCode.js", "foo" ], 308 | def: { 309 | myVal: { 310 | url: "...foo.js", 311 | minUrl: "...foo.min.js", 312 | depends: "...fooCore.js" 313 | } 314 | } 315 | } 316 | 317 | ); 318 | 319 | 320 | ### Self-executing composite script 321 | 322 | _Use the following wrapper syntax to create a composite 323 | script that can also be loaded statically_ 324 | 325 | _Note:_ if loaded statically, the individual wrapped scripts 326 | will execute in document order 327 | 328 | ((window.$deferRun || function( run ){ run(); }) ( 329 | function( $, options ) { 330 | 331 | ((window.$deferRun || function( run ){ run(); }) ( 332 | function( $, options ) { 333 | // Script code for sub script 1 here 334 | })); 335 | 336 | ((window.$deferRun || function( run ){ run(); }) ( 337 | function( $, options ) { 338 | // Script code for sub script 2 here 339 | })); 340 | 341 | ((window.$deferRun || function( run ){ run(); }) ( 342 | function( $, options ) { 343 | // Script code for sub script 3 here 344 | })); 345 | 346 | })); 347 | 348 | 349 | 350 | ### App example: 351 | 352 | #### Inline deferDef 353 | #### Delayed DomReady 354 | 355 | 356 | 357 | 396 | 397 | 398 | 419 | 420 | 421 | 5 | 6 | 7 | 8 | 9 | 24 | 25 |
26 | 27 | Results: 28 | 29 | 58 | 59 |