[dojo-contributors] deferredRequire extension

Ken Benjamin kenbenjamin at kenbenjamin.net
Sat Jun 2 12:12:42 EDT 2012


Sasha,



What you say is not correct. Everything that is an object in Javascript has
a prototype, including Function. require() is just a ordinary function
which means it is subclassed off of Function. There isn’t any way for it to
be anything else and be callable.



If you don’t believe me, load a dojo.js, open your browser debugger and
type: require.prototype. I think the confusion is that all the functions in
require() are defined inside the constructor so to see everything you would
want to look inside require.prototype.constructor.



As for your test case, it is all messed up. I don’t know how you built your
example but you are not using the code I provided. I’m attaching the
updated code with updated documentation (just rename it from .txt back to
.js before using it). See the example in there to know how to run it.



I have already integrated my updated code into the app I’m building and it
works perfectly exactly as I describe it. I know for sure it works, at
least in all the cases I’ve tested so far which include passing
dojo.config, not passing dojo.config, passing multiple modules, and passing
single modules. I can’t think of any other scenarios that are supported so
that pretty well covers being sure it works.



Ken





*From:* Sasha Firsov [mailto:suns at firsov.net]
*Sent:* Saturday, June 02, 2012 5:44 PM
*To:* Ken Benjamin
*Cc:* dojo dev.
*Subject:* Re: deferredRequire extension



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

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/cc37c23e/attachment-0001.htm 
-------------- next part --------------
require(['dojo/_base/Deferred'], function(Deferred){

	// module:
	//		hi/dojo/_base/dynamic
	// summary:
	//		Extends require() by allowing require.dynamic() to be called with dynamic runtime conditional values

	/*=====
	//	require.dynamic: function( 
		//		config,       //(object, optional) hash of configuration properties
		//		dependencies //(array of commonjs.moduleId) list of modules to be loaded before applying callback
		//	);
		//
		// summary:
		//		require() modules on demand
		//		Returns a dojo/Deferred
		//		The Deferred callback when receives the modules as arguments.
		//
		// config: 
		//		Optional: A dojo.config object
		//
		// dependencies:
		//		Required: An array of commonjs.moduleId definitions i.e. ['my/lib/Module', './MyWidget']
		//
		// returns:
		//		Deferred which on call back supplies the module objects as callback arguments. See example.
		//
		// description:
		//		require(), per the CommonJS spec, returns neither a Deferred nor the modules loaded.
		//		If you require() a module during runtime, say from a value calculated at runtime,
		//		you need another method to get that module. require.dynamic() extends require to provide
		//		that functionality.
		//
		//	example:
		//		define([
		//				'require', // get the local context require - see dojo loader for more info
		//				'dojo/_base/dynamic'
		//			], 
		//			function(require){
		//				var myModuleRequirements = ['my/lib/myModule', 'dijit/form/Button']
		//				require.dynamic(myModuleRequirements).then(
		//					function(myModule, Button){
		//						// do something with myModule and Button...
		//					}
		//				);
		//			}
		//		);	
	=====*/

	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;
	};
});


More information about the dojo-contributors mailing list