source: Dev/trunk/src/client/dojo/tests/_base/loader/asyncWithDojoRequire.html

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.5 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2   "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4  <head>
5    <title>loader-only</title>
6    <script type="text/javascript" data-dojo-config="async:true" src="../../../dojo.js"></script>
7    <script type="text/javascript">
8        function loadProvidePlugin(){
9                define("dojo/provide", ["dojo", "./text"], function(dojo, textPlugin){
10                        // summary:
11                        //              Plugin for loading legacy dojo modules (that use dojo.provide/dojo.require)
12                        //              with the async loader. For example:
13                        //              |       define(["dojo/dojo-require!dojox.cometd"], function(){
14                        //              |               dojox.cometd.init("/cometd");
15                        //              |               ...
16                        //              |       });
17                        //              This will load dojox.cometd and all it's dependencies asynchronously.
18                        dojo.require = function(id){
19                                // this is mainly for top level, it should be do nothing once a module is loaded
20                                var loadedModule;
21                                require(["dojo/provide!" + id], function(module){
22                                        loadedModule = module;
23                                });
24                                return loadedModule; // return the loaded module if it is synchronously available
25                        };
26                        dojo.provide = function(id){
27                                dojo.getObject(id, true);
28                        };
29                        return {
30                                load: function(id, parentRequire, loaded, config){
31                                        id = id.replace(/\//g, '.');
32                                        var existing = dojo.getObject(id);
33                                        if(existing){
34                                                return loaded(existing);
35                                        }
36                                        textPlugin.load(id.replace(/\./g, '/') + ".js", parentRequire, function(text){
37                                                var deps = 0, done;
38                                                var commentFree = text.replace(/\/\*[\s\S]*?\*\//g, '');
39                                                if(!/dojo\.provide\s*\(\s*['"]([^'"]*)['"]\s*\)/.test(commentFree)){
40                                                        // doesn't look like a dojo module after all, revert back to script loading (hopefully the request should be cached)
41                                                        return parentRequire([id.replace(/\./g, '/')], function(module){
42                                                                loaded(module);
43                                                        });
44                                                }
45                                                addDependencies(/dojo\.require\s*\(\s*['"]([^'"]*)['"]\s*\)/g, function(moduleId, loaded){
46                                                        parentRequire(["dojo/provide!" + moduleId], loaded);
47                                                });
48                                                addDependencies(/dojo\.cache\s*\(\s*['"]([^'"]*['"]\s*,\s*['"][^'"]*)['"]\s*\)/g, function(moduleId, loaded){
49                                                        var parts = moduleId.split(/['"]\s*,\s*['"]/);
50                                                        parts[0] = parts[0].replace(/\./g, '/');
51                                                        textPlugin.load(parts.join('/'), require, loaded, config);
52                                                });
53                                                done = true;
54                                                if(deps == 0){
55                                                        ready();
56                                                }
57                                                function addDependencies(regex, handler){
58                                                        commentFree.replace(regex, function(t, moduleId){
59                                                                deps++;
60                                                                handler(moduleId, function(){
61                                                                        deps--;
62                                                                        if(done && deps == 0){
63                                                                                ready();
64                                                                        }
65                                                                });
66                                                        });                                     
67                                                }
68                                                function ready(){
69                                                        eval(text + "\r\n//@ sourceURL=" + id);
70                                                        loaded(dojo.getObject(id));
71                                                }
72                                        });
73                                }
74                        };
75                });
76        }
77        </script>
78    <script type="text/javascript">
79                var mid = "dojo/tests/_base/loader/syncFromAsyncModule"
80                if(location.search=="?provide!"){
81                        // this just keeps the plugin out of the repo until we decide which way to go
82                        loadProvidePlugin();
83                        mid = "dojo/provide!" + mid;
84                }else{
85                        // dojo must be loaded for legacy modes to work
86                        require({async:"sync"}, ["dojo"]);
87                        // now we can switch to legacy async mode
88                        require({async:"legacyAsync"});
89                }
90                require(["dojo", "doh", mid], function(dojo, doh, syncModule) {
91                        dojo.ready(function() {
92                                doh.register("asyncWithDojoRequire", [function loadSyncModule(t){
93                                        t.is(syncModule.status, "OK");
94                                }]);
95                                doh.runOnLoad();
96                        });
97                });
98    </script>
99  </head>
100  <body>
101    <h1>Description: Async Dojo loading 1.6 module</h1>
102  </body>
103</html>
Note: See TracBrowser for help on using the repository browser.