Using "define" function in Dojo

You may experienced an error message "define is not defined" when using Dojo I got this error when trying the dojo.i18n feature when I did a dojo.requireLocalization( ) call. In some occasions a pre-built version of Dojo just don't have the "define" function at all (sigh.. ridiculous!). To solve this problem, consider the following (Dojo 1.6):

<script>
dojo.ready(function() {
    var currentModule;
    define=function(d,b,c){function g(a){if(a.charAt(0)==="."){for(a=d.substring(0,d.lastIndexOf("/")+1)+a;b!==a;)var b=a,a=a.replace(/\/[^\/]*\/\.\.\//,"/");a=a.replace(/\/\.\//g,"/")}return a.replace(/\//g,".")}c||(b?(c=b,b=d):(c=d,b=typeof c=="function"?["require","exports","module"].slice(0,c.length):[]),d=currentModule?currentModule.replace(/\./g,"/"):"anon");var e=d.replace(/\//g,"."),h=dojo.provide(e);if(typeof c=="function"){for(var i=[],a,f=0;f<b.length;f++){a=g(b[f]);var j=a.indexOf("!");if(j> -1)a.substring(0,j)=="i18n"&&(a=a.match(/^i18n\!(.+)\.nls\.([^\.]+)$/),dojo.requireLocalization(a[1],a[2])),a=null;else switch(a){case "require":a=function(a){return dojo.require(g(a))};break;case "exports":a=h;break;case "module":var k=a={exports:h};break;case "dojox":a=dojo.getObject(a);break;case "dojo/lib/kernel":case "dojo/lib/backCompat":a=dojo;break;default:a=dojo.require(a)}i.push(a)}b=c.apply(null,i)}else b=c;b&&(dojo._loadedModules[e]=b,dojo.setObject(e,b));if(k)dojo._loadedModules[e]=k.exports; return b};define.vendor="dojotoolkit.org";define.version=dojo.version;define("dojo/lib/kernel",[],dojo);define("dojo/lib/backCompat",[],dojo);define("dojo",[],dojo);define("dijit",[],this.dijit||(this.dijit={}));

    dojo.require("dojo.i18n");
    dojo.registerModulePath("language", "/path_to/language");
    // OR you can try the following if you want a relative path
    // dojo.registerModulePath("language", "../../../../../../language");
    // Suppose you are using Main.js to define the language
    dojo.requireLocalization("language","Main");
});

</script>

Reference: http://dojo-toolkit.33424.n3.nabble.com/define-is-not-defined-in-dojo-build-tt3128408.html#a3241874

Comments

Popular posts from this blog

Java encoding : UTF-8, Big5, x-MS950-HKSCS