require.undef is not synchronous with define #678

Closed
itay opened this Issue Mar 22, 2013 · 1 comment

2 participants

@itay

If I have a define call and immediately try and undefine it, then that doesn't work. For example:

<script src="http://requirejs.org/docs/release/2.1.5/comments/require.js"></script>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>

<script>
    // This will work, even though common sense says it should not
    //require.undef("jquery");
    //require(["jquery"], function(jq) {
    //    console.log(jq === $); 
    //});

    // This will give an exception:
    // Uncaught Error: Module name "jquery" has not been loaded yet for context: _. Use require([])
    //require("jquery");
    //require.undef("jquery");

    // This will kind of work, but will still cause an exception:
    //GET http://10.80.1.185:8000/appfx/static/testapp/jquery.js 404 (NOT FOUND) require.js:1854
    //Uncaught Error: Script error
    require(["jquery"]);
    require.undef("jquery");
</script>

The specific case where this happens to me is in Django, when I use the Django Debug Toolbar. They include their own version of jQuery, and use jQuery.noConflict(true) to have it not interfere with the jQuery that may already be on the page.

However, jQuery has its own call to define:

define( "jquery", [], function () { return jQuery; } );

which I would like to undefine, otherwise my calls to require or define end up picking up this jQuery, which is not what I want. However, as noted above, there is no way for me to undefine it synchronously, so I have to do something like:

require(['jquery'], function() {
  require.undef('jquery');
});

which seems both silly and dangerous, because something else still has time to pick up the bad jquery value.

@jrburke
requirejs member

Why not use the first version you mention:

require.undef("jquery");
require(["jquery"], function(jq) {
    console.log(jq === $); 
});

This works because the script tag jquery has called define(), so the undef gets rid of it, and then you can continue with the load of jquery that you want.

I think this ticket is more about why things work they way they do than a specific code bug, so closing as part of bug triage. However, feel free to continue discussion and we can reopen if we uncover something to be fixed.

@jrburke jrburke closed this Mar 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment