Derek
Derek

Reputation: 12388

Questions about Dojo modules from Dojo newb?

I'm new to Dojo so I don't quite understand all of Dojo's features. Here are some questions, but I'm sure some of them seem really stupid since I don't quite get how Dojo is structured yet:

  1. How do you create multiple modules within a single js file and access the module in the file it's created? Also, how do you access specific modules from a file containing multiple modules?

  2. What is the difference between require and define?

  3. I successfully required a module from a file, but I can't figure out how to get the variables out of the file, how do you do this?

  4. I was looking at how Dojo required its modules and notice that it does a http request for each file, but isn't that really inefficient when you're dealing with lots of modules and/or on a large site, you really would like to minimize the number of http requests necessary? What's the way around this?

Upvotes: 2

Views: 1457

Answers (2)

hugomg
hugomg

Reputation: 69984

  1. How do you create multiple modules within a single js file?

    While this is possible to do, and is done by the build system when it creates layer files, it is not recommended. Putting multiple modules in a single file means you have to use a different form of define that gives an explicit ID to eahc of them. This is less flexible then having the module IDs automatically derived from the file name and path (this, together with relative paths makes it much easier to move and rename AMD modules, compared to old-style modules)

  2. What is the difference between require and define?

    define is a beefed up version of require that defines a new module with its return value.

  3. I successfully required a module from a file, but I can't figure out how to get the variables out of the file.

    I am not sure wha toyu mean with this (without giving a concrete runnable example) but all you have to do is create an object to be the value of your module and return it from define. This is not very far off from how you would define modules the old way, with manual "namespaces"

    //foo.js
    define([], function(){
    
        var M = {}; //the exported stuff will go here
    
        var myVariable = 16; //private function variables are private (like they are everywhere else)
                             //and cannot be seen from the outside
    
        M.anumber = myvariable + 1; //things on the returned object can be seen from the outside
    
        return M; //people who  require this module will get this return value
     });
    
    //bar.js
    define(['foo'], function(foo){
        console.log( foo.anumber );
    });
    
  4. I was looking at how Dojo required its modules and notice that it does a http request for each file...

    As phusick pointed out, the build system can be used to bundle all the modules into a single file (giving you the best of both worlds - modularity during development and performance during deployment). It can also do other cool stuff, like bundling CSS and passing the Javascript through a minifier or through the Closure compiler, checking ifdef for creating platform-dependent builds, etc.

Upvotes: 1

phusick
phusick

Reputation: 7352

Reading through The Dojo Loader will provide answers.

Basically module = file and very often (as a matter of best practice) module = file = class (more precisely public class defined via dojo/_base/declare).

Ad #4: You will need to employ The Dojo Build System, that will resolve all the dependencies and put all your modules into a single file (or more files, this depends on your build profile). Have a look at Dojo Boilerplate project, it may help with building your application.

Upvotes: 4

Related Questions