Monday, December 20, 2010

RequireJS 0.2.1 Released

RequireJS 0.2.1 is available for download.

This is a bug fix release related to issues with the refactoring done in 0.2.0. Probably the most noticeable bug that has been fixed: the "all plugins" builds did not include the plugins.

9 comments:

spiderman said...

I used convertDojo.js to convert dojo 1.5 source. The conversion is a success after removing "color.js" under nls subdirectory.

However, I couldn't use the converted dojo library. The error message say: "require.modify is not a function" (line 641 in dojo.js).

I then realize that this feature has been removed since 0.2.0. But it seems that convertDojo script still emit it.

How can I fix this? Thanks in advance.

By the way, I also need to use gfx library and there is a separate "dojoxgfx.js" script which I have no idea how to use it. What do I need to do to use converted gfx library?

James Burke said...

spiderman: that conversion script is a bit dated, I have not kept it up with the latest developments.

A better option might be to try neonstalwarts's dojo-requirejs-template.

It runs against Dojo trunk, and I am not sure how much of dojox has been converted to work with RequireJS.

spiderman said...

James, thank. I just took a look of that template and it does not include gfx module and does not provide instruction on how to convert that. so I guess I have to dig deeper in that template or look elsewhere...

I hope that dojo can be converted to use requirejs as soon as possible in future releases as this is such an important feature.

spiderman said...

James, how could I programmatically load a module and wait for it before proceeding?

1. lafName = getLookAndFeelClassName().
2. require([lafName], function(lafClass){});

Thank you in advance.

spiderman said...

After a lot of googling, reading, trial and error. I am answering my own question here: I just need to create a module/class, say, 'LookAndFeel' as below:

define(function(require){
var laf;

return {
create: function(lafName) {
if ('basic' == lafName){
laf = require('diagram/plaf/basic/BasicLookAndFeel');
}else if('metal' == lafName){
laf = require('diagram/plaf/metal/MetalLookAndFeel');
}
return laf;
}
};
});

James Burke said...

spiderman: sorry for the late response. Your define example syntax will result in both 'diagram/plaf/basic/BasicLookAndFeel' and 'diagram/plaf/metal/MetalLookAndFeel' being fetched before executing that define function.

If you are OK with both modules being fetched up front before anything calls create, then that works fine.

If you want it to be a runtime decision, then the previous example you gave is better:

require([lafName], function(lafClass){});

but it does mean that is an async fetch, so you would have to change your "create" method to probably accept a callback to call after the right module is fetched.

spiderman said...

James, thanks for the reply. I actually want it to be a runtime decision. As a result, I have re-written my code.

The reason that I used an "upfront" decision earlier is simply because of my incapability of understanding how requirejs works.

I thought I could use 'require(lafName)' as a synchronous call by substituting constant class name with a 'lafName' variable. But this obviously does not work since a callback function is required.

Another 'odd' thing I that found out is that if I want to utilize this 'dynamic require' capability, I wouldn't be able to obtain any other 'static classes' like below:

define(["classA", "classB"], function(classA, classB, require){
return xxx;
}

I am wondering why this is so? Please enlighten me. Thanks.

James Burke said...

spiderman: I am not sure I follow your question, but this example would allow you to get some static modules up front then ask for something dynamic later, just be sure to ask for 'require' as a dependency:

define(['require', 'ClassA', 'ClassB'], function (require, ClassA, ClassB) {
return {
loadNextModule: function (moduleName, callback) {
require([moduleName], callback);
}
});

spiderman said...

James, that's exactly what I want. I guess I did not specify "require" as one of the dependencies on my previous attemps. I'll try it out later. Thanks.