简体   繁体   中英

How can I use custom Dojo modules in the proper AMD way outside the initial function?

I finally understood that and why named classes are not a good practice and I would like to update my code. However, even after reading several documentation pages , blog entries and SO questions , I don't understand how to do it in the proper AMD way when I need to use the module outside the function where the modules are required. Do the AMD support this, or should I completely rearrange the file from where I use the AMD class?

What I did before - configuration:

var dojoConfig = {
    paths: {
        fooBar: '@Url.Content("/portal/Modules/Foo.Bar/Scripts/foo-bar")'
    }
};

In the module:

define("fooBar/foo", [/*required modules*/], function(/*required modules*/) {
    //the rest of code

return declare("fooBar.foo", [/*superclass*/], {
    //the rest of code

And in the file where I use it:

var required = [
    /*first 5 required modules*/
    "fooBar/foo",
    /*the rest of required modules*/
]

require(required, function (/*5 first modules*/, Foo) {
    //code

    var barBaz = _customFunction(/*parameters*/);

    //code
});

//Foo and other names set by require are not available here
function _customFunction(/*parameters*/) {
    //code

    var foo = new fooBar/foo({/*parameters*/});

    //code
}

I removed the module name from the class and I can't see it no more. Even the fooBar is undefined unless I require named function bar from the same namespace. I added async: true to the config, but nothing changed. I understand that AMD class constructors shouldn't be including in the function where the module is required (don't ask me for source, somewhere linked above), but how to make an object of the class?

What should work is another call of require, but I'm not sure how to use it. What's, I'm not sure whether this is considered good practice - making some dirty workaround would break the point of moving to the unnamed declare.

What works is to put everything into a wrapper function, or possibly into a class, where no code lies outside the scope of the require statement. In my case I moved the function _customFunction to a named class with an empty constructor to avoid wrapping and possibly breaking tens of functions residing in the same file; we will transform the others as well once we have time... and be sure we are really improving the code.

So, the fish is caught, but I'm still stuck halfway in this fishing lesson.

TL;DR: I need to understand what is good practice and what is not-so-good practice but still works (and why) for transcribing class constructor calls residing outside the scope of the names set by the require statement.

The seemingly only Dojo tutorial I hadn't read before asking this question contains the right advice for me!

Key parts:

Again, repeat after me "the global namespace is bad, the global namespace is bad, I will not use the global namespace, I will not use the global namespace".

So wrap it inside require statement.

Within the closure of the require(), we reference the modules based on the variable name we declared in the argument.

This could be within a declare , but it's not necessary.

The other core function in AMD is define() which is usually used for defining modules. See the Defining Modules tutorial for more information on how to use define().

So even though there still are few technical details I'm not sure of, now I understand the basic idea and I know what to look for in the next step. The rest should be easy. I may update to improve this answer even more later to help others with the same problem, but I am happy now :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM