[dojo-contributors] deferredRequire extension

Sasha Firsov suns at firsov.net
Sat Jun 2 11:44:08 EDT 2012


Nice idea, but in DTK require() is not regular object with prototype

require.prototype

would work only inside of your "dojo/_base/dynamic" and would not be 
populated within custom methods "require" :(
See that on
http://simulationlabs.com/dojo/DojoPlay2012/libcommon/DTK1.7/util/doh/runner.html?testModule=lib/AMD/test/deferredRequireTest&paths=lib,../../../lib 
<http://simulationlabs.com/dojo/DojoPlay2012/libcommon/DTK1.7/util/doh/runner.html?testModule=lib/AMD/test/deferredRequireTest&paths=lib,../../../lib>

lib/AMD/test/deferredRequireTest.js 
<http://simulationlabs.com/dojo/DojoPlay2012/libcommon/DTK1.7/util/doh/runner.html?testModule=lib/AMD/test/deferredRequireTest&paths=lib,../../../lib> 
has test case for require.dynamic.

What could potentially work, is AMD post-processor since it will accept 
pre-made module:

define( ["require!dynamic"],function( require )
{
     require(["myMod"]).then( function(myMod){} );
}

Let me check postprocessor availability...
Sasha

PS. why you trying to substitute arguments in callback? You could use it 
as is:
         require.prototype.constructor.dynamic = function()
         {
                 var d = new Deferred();
                 var callback = function()
                     {
                         d.resolve(arguments);
                     };
                 require.apply(this, arguments);
                 return d;
         };


On 2012-06-02 01:20, Ken Benjamin wrote:
> How about this, which extends all instances of require() to have
> require.dynamic(). Parameters are the same as for require() excluding a
> callback but the return is a Deferred.
>
> Proposed dojo/_base/dynamic.js:
>
> require(['dojo/_base/Deferred'], function(Deferred){
> 	require.prototype.constructor.dynamic = function(
> 		config,       //(object, optional) hash of configuration
> properties
> 		dependencies //(array of commonjs.moduleId) list of
> modules to be loaded before applying callback
> 	){
> 		var d = new Deferred();
> 		var callback = function(){
> d.resolve(Array.prototype.slice.call(arguments)); }; // callback Deferred
> with list of modules		
> 		require.apply(this, (config instanceof Array) ? [config,
> dependencies, callback] : [dependencies, callback]);
> 		return d;
> 	};
> });
>
> Example use:
>
> require([
> 		'require', // local context require
> 		'dojo/_base/dynamic'
> 	],
> 	function(require){
> 		var widgets = ['dijit/form/Button', 'dijit/form/Textbox'];
> 		require.dynamic(widgets).then(function(Button, Textbox){
> 			// do something with Button and Textbox
> 		});
> 	}
> );
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.dojotoolkit.org/pipermail/dojo-contributors/attachments/20120602/29360f2f/attachment.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3861 bytes
Desc: S/MIME Cryptographic Signature
Url : http://mail.dojotoolkit.org/pipermail/dojo-contributors/attachments/20120602/29360f2f/attachment.p7s 


More information about the dojo-contributors mailing list