Search code examples
javascriptyuidependency-managementyui3

YUI3 Dependency management


I am trying to develop a web application leveraging the YUI 3 app framework and am not able to figure out how the dependancy management actually works.

I wish to define a module that I want to reuse for another location in a different file.Here is the code I use to define the module in some file dir1/home/home.js:

YUI.add('MyModule_Home',function(Y){
    Y.namespace("mysite.home");
    Y.mysite.home.SomeView = Y.base.create("SomeView", Y.View, {
        initializer : function() {
            // some logic goes here ... 
        }
        // more functions go here ....
    }
},'0.1',{ requires : ['base','node','app'] });  // <---- Dependency Specifications

In another File (index.js) to use this module I use :

YUI( {
  groups: {
    grp1: {
      base: "/path/to/dir1/",
      modules: {
        MyModule_Home: {
          path: "home/home.js",
          requires: ['base', 'node', 'app'] // <------ Dependency Specifications
        }
      }
    }
  }
})

The above code works, but I would like to know why are the redundant dependency specifications required at both places. If I omit the dependency specification while loading the module or while defining the module I get errors. Is there a more succinct way of doing this ?


Solution

  • Without specifying the dependencies up front in the config object you pass to YUI() it has no way of knowing what modules it needs to get before it can load your MyModule_Home module. If you don't specify the metadata in the config object it should go load your module, read the requirements, and then go load them. This is sub-optimal performance-wise but can be a workable solution while developing.

    Just make sure to go back & fix it later!

    The YUI team uses the builder to wrap their modules in YUI.add calls & that handles the metadata for those modules. I find that to be an unnecessary heavy workflow so I maintain the metadata in two places. It ends up not being that large a maintenance cost in my experience.