[483] | 1 | <!DOCTYPE html> |
---|
| 2 | <html> |
---|
| 3 | <head> |
---|
| 4 | <title>Parser Asynchronous Widget Creation Unit Test</title> |
---|
| 5 | <style type="text/css"> |
---|
| 6 | @import "../../resources/dojo.css"; |
---|
| 7 | </style> |
---|
| 8 | <script type="text/javascript" src="../../dojo.js" data-dojo-config="isDebug:true, async:true"></script> |
---|
| 9 | <script type="text/javascript"> |
---|
| 10 | |
---|
| 11 | require([ |
---|
| 12 | "doh", |
---|
| 13 | "dojo/_base/array", "dojo/_base/declare", "dojo/Deferred", "dojo/dom", "dojo/_base/lang", "dojo/parser", |
---|
| 14 | "dojo/domReady!" |
---|
| 15 | ], function(doh, array, declare, Deferred, dom, lang, parser){ |
---|
| 16 | |
---|
| 17 | // instances of AsyncWidget will finish initializing when this Deferred is resolved |
---|
| 18 | var finishCreatingAsyncWidgets = new Deferred(); |
---|
| 19 | |
---|
| 20 | AsyncWidget = declare(null, { |
---|
| 21 | declaredClass: "AsyncWidget", |
---|
| 22 | markupFactory: function(params, node){ |
---|
| 23 | // the markup factory can return a promise, and the parser will wait |
---|
| 24 | return finishCreatingAsyncWidgets.then(function(){return new AsyncWidget(params, node); }); |
---|
| 25 | }, |
---|
| 26 | constructor: function(args, node){ |
---|
| 27 | this.params = args; |
---|
| 28 | lang.mixin(this, args); |
---|
| 29 | }, |
---|
| 30 | startup: function(){ |
---|
| 31 | this._started = true; |
---|
| 32 | } |
---|
| 33 | }); |
---|
| 34 | |
---|
| 35 | SyncWidget = declare(null, { |
---|
| 36 | declaredClass: "SyncWidget", |
---|
| 37 | constructor: function(args, node){ |
---|
| 38 | this.params = args; |
---|
| 39 | lang.mixin(this, args); |
---|
| 40 | }, |
---|
| 41 | startup: function(){ |
---|
| 42 | this._started = true; |
---|
| 43 | } |
---|
| 44 | }); |
---|
| 45 | |
---|
| 46 | doh.register("async tests", [ |
---|
| 47 | function parse(){ |
---|
| 48 | var d = new doh.Deferred(); |
---|
| 49 | |
---|
| 50 | // Call the parser |
---|
| 51 | var parsePromise = parser.parse(dom.byId("main")); |
---|
| 52 | |
---|
| 53 | // Parse should be waiting for the async widget to finish creating |
---|
| 54 | doh.f(parsePromise.isFulfilled(), "parse not finished yet"); |
---|
| 55 | doh.is("undefined", typeof asyncWidget, "async widget not created yet"); |
---|
| 56 | doh.f(syncWidget._started, "sync widget created by not started"); |
---|
| 57 | |
---|
| 58 | // Now make the async widget finish initializing |
---|
| 59 | finishCreatingAsyncWidgets.resolve(true); |
---|
| 60 | |
---|
| 61 | parsePromise.then(d.getTestCallback(function(list){ |
---|
| 62 | doh.t(asyncWidget._started, "async widget started"); |
---|
| 63 | doh.t(syncWidget._started, "sync widget started too"); |
---|
| 64 | doh.is("AsyncWidget, SyncWidget", array.map(list, function(cls){ return cls.declaredClass; }).join(", "), |
---|
| 65 | "list of instances returned from parser"); |
---|
| 66 | })); |
---|
| 67 | |
---|
| 68 | return d; |
---|
| 69 | } |
---|
| 70 | ]); |
---|
| 71 | |
---|
| 72 | doh.run(); |
---|
| 73 | }); |
---|
| 74 | </script> |
---|
| 75 | </head> |
---|
| 76 | <body> |
---|
| 77 | <h1>Parser Asynchronous Widget Creation Unit Test</h1> |
---|
| 78 | |
---|
| 79 | <div id=main> |
---|
| 80 | <span data-dojo-id="asyncWidget" data-dojo-type="AsyncWidget">hi</span> |
---|
| 81 | <span data-dojo-id="syncWidget" data-dojo-type="SyncWidget">there</span> |
---|
| 82 | </div> |
---|
| 83 | |
---|
| 84 | </body> |
---|
| 85 | </html> |
---|