New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested define/require inside another require does not see defined module #173
Comments
|
I'm finding my nested dependencies are passed as undefined. I've used jQuery twice in this sample simply to demonstrate, however in my actual code, I am not attempting to call the same dependency on the inner define. What am I missing in order to have the variable defined on the inner define? |
|
Nested defines like that would not make sense. define() is for defining a module. For grabbing a module while inside a define's factory function, require([]) should be used instead. |
|
I tried various implementations. I was unable to get it to work. Basically, I am attempting to dynamically generate dependencies for the inner function. Is it possible to pass a set of additional dependencies to a nested function? |
|
You can use require([]) to dynamically load things inside a define() call. But note that require([]) is async, so it will call the function callback in a later turn, the loader will not call the callback function before the define() factory function completes execution. define(['require', 'jquery'], function (require, jquery) {
require(['something/else'], function (somethingElse) {
});
}); |
|
That worked beautifully! |
|
Hi, I need something like that but a syncronous require. Stop execution and continue after get somethingElse. I can't be sure while I'm loading a module the user ask for another one. Or the developer try to use somethingelse after that. |
|
@patrixd Synchronous require that does a fetch is not supported. What is possible is preventing further require() calls by waiting on the result of the first call using a mechanism similar to this: define(['require', 'a'], function (require, a) {
var queue = [];
var fetchingId, cb;
function fetched(moduleValue) {
cb(moduleValue);
// Now decide if the other items in the queue
// should be fetched or discarded.
}
function whenClear(id, callback) {
if (!fetchingId) {
fetchingId = id;
cb = callback;
require([id], fetched);
} else {
queue.push([id, callback]);
}
}
return {
loadSomething: function (id, callback) {
whenClear(id, callback);
}
}
});Where the module's loadSomething is used by outside code. |
|
Thank you for your help. define(['require', 'jquery'], function (require, jquery) {
require(['something/else'], function (somethingElse) {
});
});And when the site is loaded there is a 404 error telling that something/else was not found. Do you know what I can do? Thanks in advance. |
|
By default, nested require([]) calls inside a define() call are not included in a build as it indicates something that should normally be dynamically loaded. However you can override this by using |
|
Thank you so much! I didn't know it! I'm going to add that in my config just now. =) |
|
@jrburke I had the same wish to have dynamic and synchronous file loading. While trying out various define/require nested constructions, I finally found your statements on this issue. I have a question concerning the mechanism in your 'loadSomething' example. I find out that a require call in the remaining id/cb pairs within the queue is not synchronous anymore. So why we have a synchronous require in the very first call? |
|
I have been running into an issue on windows, which I can only attribute as some type of race condition and it sounds like it is related to this. We have code: define([ .. modules ], function( ... ) { in the block of now use somevar, somevar ends up sometimes being undefined. This happens on window :/ while running tests. Assuming initialize() is called before render() and somevar is set, why would this sometimes be null? |
|
nice thread.. |
|
I wish to get an object from the callback in require and return it as defined module, e.g. How can I solve this problem. |
|
You should not add nested resolution for deps. Push them to top. define(['require', 'jquery', 'something/else'], function (require, jquery, somethingElse) {
console.log(somethingElse); //expected object is defined
somethingElse.$ = jquery; // or something else
return somethingElse;
}); |
|
thanks @FoC- got it working. |
|
Hi @jrburke i just recognised in 2.3.6 version. Require not able to get depend as i expected if i put inside function define(['require', 'abc', 'something/else'], function (require, abc, somethingElse) {
//working fine here
someVar = {
init: function () {
require(['package'], function(package) {
//package not able to work. I got some message undefined call to something
});
},
update: function () {}
}
});For requirements i don't want to put package file to top define. Only require it on demands so i put it in side deeper scope For real example please reference this link |
Found in a test case by @dvdotsenko in this set of tests https://github.com/dvdotsenko/AMDLoaderTests :
The text was updated successfully, but these errors were encountered: