cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

How to implement queueScript function? #123

Closed warmrobot closed 11 years ago

warmrobot commented 11 years ago

Hello!

I am using LAB.js in most of my projects, but I am very interesting about AMD. I prefer bulletproof way to construct a chain (queue) and run it at the very end of the page, before /body, because there are inline scripts on different pages and it is very nice trick to handle with them. I mean this nice function: http://labjs.com/documentation.php#queuescript (see example #9 on this page also)

Can I write the same in cujo.js?

Thank you!

unscriptable commented 11 years ago

Hello @warmrobot!

You could probably write just about anything with curl.js. :) However, I'm not sure you really need to implement a queueScript process if you're using AMD correctly. curl.js automatically queues modules according to their dependencies.

Or maybe I just don't understand the use case. (???)

-- John

warmrobot commented 11 years ago

Oh! Sorry! curl.js, of course )))

OK, I try to explaine one more time:

I don't need to start load scripts immediately. I want to consctruct a chain, and execute it at the end of document. With all dependencies, async and all.

For example, if write in the HEAD

curl(['js!nonAMD.js!order', 'js!another.js!order']), function () {
    /* do something cool here */
});

and then write in the middle of the document (inline script)

curl(['js!third.js!order']), function () {
    /* I am very fast and load independently of scripts in head */
});

there is no garantees, that third.js will load AFTER two scripts in the head, yes?

warmrobot commented 11 years ago

Kyle Simpson propose to make an array, and execute everything in it:

var queue = [];
...
if (conditionOne()) { 
   queue.push("framework.js",false); // false will create an empty .wait()
}
...
if (conditionTwo()) {
   queue.push("plugin1.js","plugin2.js",function(){
      // initialize plugins
   });
}
...
queue.push("mypage.js");
...
var $L = $LAB;
for (var i=0, len=queue.length; i<len; i++) {
   if (typeof queue[i] === "function") $L = $L.wait(queue[i]);
   else if (queue[i] === false) $L = $L.wait();
   else $L = $L.script(queue[i]);
}

http://blog.getify.com/simulated-chaining-in-javascript/

And then he includes this feature as queueScript in new version of LAB.js. And it is very useful. So, I interesting about this feature (or recipe) in curl.js too.

unscriptable commented 11 years ago

That should work! If your example doesn't work, then there's a bug. I just looked at the code and the !order option should make scripts wait no matter when they're called, iiuc.

curl(['js!nonAMD.js!order', 'js!another.js!order']), function () {
    /* do something cool here */
});

and then write in the middle of the document (inline script)

curl(['js!third.js!order']), function () {
    /* I am very fast and load independently of scripts in head */
});

Have you tried this? Let me know! :)

The queueScript idea is kinda fun, but also goes against cujo's principles. Specifically, we strongly encourage use of modular javascript over non-modular. The features in curl.js that assist in non-modular code are there to assist devs who still have a large codebase of non-modular files.

AMD and CJS have other patterns for solving dependency management and the initialization/startup/bootstrap process. So this feels like something that's only really useful for non-modular code. In short: it'll probably never make it to the top of the priority list. :(

However, if you feel like writing a curl/shim module (a module that changes the behavior of curl), I could offer tips and support.

-- J

warmrobot commented 11 years ago

Yes, you right, I have many non-modular scripts. ( OK, I think I should try different schemas to find suitable for me. Thank you!

unscriptable commented 11 years ago

If you decide to make your code more modular, we'd love to help at the cujojs group: https://groups.google.com/d/forum/cujojs

:)

warmrobot commented 11 years ago

Well, most of them are jQuery UI plugins. I don't khow is it good idea to make them AMD compatible. I like to stay up to date with them.