[dojo-contributors] deferredRequire extension

Ken Benjamin kenbenjamin at kenbenjamin.net
Sat Jun 2 14:09:35 EDT 2012


Well, that was Sasha’s initiative.



I originally put the idea up as an enhancement ticket for consideration by
the contributors, as it’s a function that seems to be needed in some cases
and, as you point out, isn’t part of AMD spec. See the first two messages
in this thread for somewhat more productive information.



The idea is that it ought not to be core to AMD (unless that group decides
to make it so) but an extension to require() for those who want it.



I’ll update my enhancement ticket with better code tomorrow and leave it at
that for my part, which was my original plan.



I’ll take the rest of the dialog with Sasha about how it works (or doesn’t)
off the group.



Sorry to have inconvenienced you,

Ken





*From:* Tom Trenka [mailto:ttrenka at gmail.com]
*Sent:* Saturday, June 02, 2012 7:58 PM
*To:* dojo dev.
*Cc:* Ken Benjamin
*Subject:* Re: [dojo-contributors] deferredRequire extension



Why is this on the DTK contributor list?  The AMD spec is somewhat well
defined, and it does not expect any kind of promise/Deferred as part of
that spec.  I can appreciate the efforts but this is not the place to
discuss it.  Perhaps the AMD list?



http://groups.google.com/group/amd-implement



Hope that helps,



Tom



On Sat, Jun 2, 2012 at 12:17 PM, Sasha Firsov <suns at firsov.net> wrote:

On 2012-06-02 09:12, Ken Benjamin wrote:

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.

I know about JS basics. The point was that scope where require function is
taken from is not same, it is dynamic. As result each module has own
"require". You tricked it by overriding "global" require.prototype.
( in your patch definition there is no declare dependencies on "require".)
Which is given fixed context(used for module path calculation). As result
no relative pathes are working.




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.

Did that. And placed your code as is with as seems valid test:
http://simulationlabs.com/dojo/DojoPlay2012/libcommon/DTK1.7/util/doh/runner.html?testModule=lib/AMD/test/requireDynamicTest&paths=lib,../../../lib

Test works fine with absolute path.
Test did not pass relative path :(




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.

The #1 test was for your implementation. To make it clear see the link
above - only your code is in place.




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.

Have you tried relative path-es?
Sasha




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

         });

  }

);








_______________________________________________
dojo-contributors mailing list
dojo-contributors at mail.dojotoolkit.org
http://mail.dojotoolkit.org/mailman/listinfo/dojo-contributors
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.dojotoolkit.org/pipermail/dojo-contributors/attachments/20120602/0540aded/attachment-0001.htm 


More information about the dojo-contributors mailing list