Updating Dojo provide

ε祈祈猫儿з 提交于 2019-12-24 02:06:11

问题


I am using Dojo 1.9 for a project, but I don't understand the correct alternative of dojo.provide in the AMD style as compared to the legacy style. I was reading this documentation page.

So apparanently this is how the old syntax maps to the new one:

OLD

dojo.provide("acme.Dialog");
dojo.require("dijit._Widget");
dojo.require("dojo.date");
CODE HERE

NEW

define(["dijit/_Widget", "dojo/date"], function(_Widget, date){   ....
     CODE HERE
     return MyWidget;
});

I am not sure what exactly am I returning as MyWidget. My code looks something like this:

define(["dojo/foo/x","dojo/foo/y"], function(x, y){
    dojo.provide("my.module");     
});

What am I supposed to return here?


回答1:


There is no equivalent to dojo.provide in AMD. This call was only ever necessary to inform parts of the system that you intend to create an object at a given place in the global scope from within the file. Now we use AMD module IDs, which are based on filenames, to do that.

From within the AMD module’s factory function, you may return anything, or you may return nothing. If you return something, it becomes the value of the module. So, in legacy:

A module that defines nothing:

dojo.provide("app.nothing");
// some code

would become:

// in app/nothing.js
define([], function () {
    // some code
});

A module that defines an object:

dojo.provide("app.myModule");
app.myModule = { foo: "foo" };

would become:

// in app/myModule.js
define([], function () {
    return { foo: "foo" };
});

And a module that defines a constructor using declare:

dojo.provide("app.MyClass");
dojo.require("dijit._WidgetBase");
dojo.declare("app.MyClass", dijit._WidgetBase, {});

would become:

// in app/MyClass.js
define([ "dojo/_base/declare", "dijit/_WidgetBase" ], function (declare, _WidgetBase) {
    return declare(_WidgetBase, {});
});



回答2:


Here's a greally great tutorial, "Modern Dojo":

http://dojotoolkit.org/documentation/tutorials/1.7/modern_dojo/

Good quote:

If you find yourself typing dojo.* or dijit.* or dojox.*, something isn't right.

Basic rationale for the changes to AMD:

One of the core concepts of "modern" Dojo is that things in the global namespace are bad. There are numerous reasons for this, but in a complex web application, the global namespace can easily become polluted with all manner of code, especially when a lot of organisations use multiple JavaScript frameworks. I won't even mention the nefarious things that can happen from a security standpoint with people intentionally modifying the global namespace. This means in "modern" Dojo, if you are about to access something in the global namespace STOP because you are doing something wrong.

...

Another core concept is that synchronous operations are slow and asynchronous ones are usually faster. "Legacy" Dojo already had a fairly strong pedigree in asynchronous JavaScript code with the concept of dojo.Deferred, but in "modern" Dojo, it is best to think of everything operating asynchronously.

...

To strengthen the modularity of Dojo and leverage the concepts above, in 1.7 Dojo adopted the CommonJS module definition called Asynchronous Module Definition (AMD). This meant a fundamental re-write of the Dojo module loader which is usually exposed through the require() and define() functions. You can find full documentation of the loader in the reference guide.. This has fundementally changed the way code is structured.



来源:https://stackoverflow.com/questions/19504147/updating-dojo-provide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!