Skip to content Skip to sidebar Skip to footer

Dojo Cyclic Dependency Using Require

I'm using dojo and these are my modules - 'myview/ModuleA' --requires--> 'myview/ModuleB' 'myview/ModuleB' --requires--> 'myview/ModuleC' 'myview/ModuleC' --requires--> 'm

Solution 1:

Requiring "on the fly" instead of doing it a define time is the proper approach for circular dependencies. This is explained here: http://requirejs.org/docs/api.html#circular

If you define a circular dependency ("a" needs "b" and "b" needs "a"), then in this case when "b"'s module function is called, it will get an undefined value for "a". "b" can fetch "a" later after modules have been defined by using the require() method (be sure to specify require as a dependency so the right context is used to look up "a")

Important: At build time, you will have to specify in the build profile all components you had to require on the fly. Otherwise they will not be included in the list of files to build.

Solution 2:

Even though RequireJS has a proper workaround for circular dependencies like this:

//Inside b.js:define(["require", "a"],
    function(require, a) {
        //"a" in this case will be null if "a" also asked for "b",//a circular dependency.returnfunction(title) {
            returnrequire("a").doSomething();
        }
    }
);

(Source)

Using circular dependencies often* means that you have a design that should be improved. Having 2 modules that depend on each other means that you have a highly coupled design, which should ring a bell in every developers brains. Read this for more information: https://softwareengineering.stackexchange.com/questions/11856/whats-wrong-with-circular-references

The proper solution would be that if A depends on B and B depends on A, is that all code that B uses from A is separated in a different module, which we call C. We can then decouple the modules like this:

  • A depends on B
  • A depends on C
  • B depends on C

And there you have it, you have decoupled your code by using an intermediator.


* Cyclic dependencies are not always bad though, for example, if you have a Company that has Employee's, then you could also say that you have an Employee that works within a Company. In this case you would have a circular dependency as well, and then you can use the approach described above.

Post a Comment for "Dojo Cyclic Dependency Using Require"