Select to view content in your preferred language

Call custom modules within modules

1449
9
Jump to solution
10-21-2013 03:56 PM
BenFousek
Deactivated User
I have two modules in a folder called "mods", which is loaded as a package with dojoConfig.  I require ModuleA in ModuleB, and it loads.  But I can't use it as a variable of the define function, only by a legacy call (see code).  If I require ModuleA in the application I can use it normally.  Am I missing something?  Any help is appreciated.

ModuleA:
define([  'dojo/_base/declare' ], function (declare) {   return declare('mods.ModuleA', [], {     //create module   }); });


ModuleB:
define([  'dojo/_base/declare',  'mods/ModuleA' ], function (declare, ModuleA) {   return declare([], {     //create module          createA: function() {              console.log(ModuleA); //returns an interger?!?!              console.log(mods.ModuleA); //returns module function()              /*       this.container.addChild(new ModuleA({a: 1, b: 'hi'}));       // throws an error because ModuleA = some integer       */              this.container.addChild(new mods.ModuleA({a: 1, b: 'hi'})); //legacy works?!?!            }        }); });
0 Kudos
1 Solution

Accepted Solutions
MattDriscoll
Esri Contributor
Hey Ben,

Looks like it's not working right because you're using require() instead of define() in the custom module. Change it to define() and it should work.

View solution in original post

0 Kudos
9 Replies
JohnGravois
Deactivated User
im a little out of my depth here, but perhaps in your widgets you should be declaring a variable and 'return' it instead of returning your declare statement directly?  (ie like driskull does in his AMD home button widget).

https://github.com/driskull/arcgis-dijit-home-button-js/blob/master/js/HomeButton.js
0 Kudos
BenFousek
Deactivated User
Thanks John. I did start declaring and returning a variable.  Still haven't worked it out.  In this case, I'm creating a custom layer and I think there's an issue with how I'm declaring it.  Not having that problem with other classes and widgets.  I'm sure the solution will come to me sooner or later.
0 Kudos
JohnGravois
Deactivated User
maybe worth it to try and deminify and dissect our directions widget and try to compare and contrast?  i'm fairly sure it relies on the geocoder widget.
0 Kudos
MattDriscoll
Esri Contributor
Most likely, either the widget is not getting assigned to the correct variable or the module path is incorrect.

I've updated my simple dijit sample to load a custom module from within another.

Here's the code:
https://github.com/driskull/arcgis-dijit-sample-js
http://driskull.github.io/arcgis-dijit-sample-js/

This is how I added the custom module:
https://github.com/driskull/arcgis-dijit-sample-js/commit/23368142f9e8537fb919d0d9aeeae05c8ab785c7
0 Kudos
BenFousek
Deactivated User
Still haven't figured it out. This is where I'm at:

Custom layer:
require(['dojo/_base/declare', 'esri/layers/layer', 'esri/layers/TileInfo', 'esri/geometry/Extent'], function (declare, layer, TileInfo, Extent) {
  var customLayer = declare('modules.CustomLayer', layer, {
    declaredClass: 'modules.CustomLayer',
    constructor: function (params) {
      params = params || {};
      this.tileInfo = new TileInfo( /*tile info*/ );
      this.fullExtent = Extent( /*extent*/ );
      this.initialExtent = Extent( /*extent*/ );
      this.loaded = true;
      this.onLoad(this);
    }
    //functions blah blah blah
  });
  return customLayer;
});


Using it:
require(['dojo/_base/declare', 'esri/map', 'modules/CustomLayer', 'dojo/domReady!'], function (declare, Map, CustomLayer) {
  var map = new Map('map-div', {
    //map params
  });

  console.log(CustomLayer); //returns integer 3
  console.log(modules.CustomLayer); //returns function()

  map.addLayer(new CustomLayer()); //fail

  map.addLayer(new modules.CustomLayer()); //works
});


I don't understand why I need to set the className argument to even access the module, and I don't understand why I can't use the variable, but instead have to call the class itself. I'm not having to set className with templated widgets or other classes.
0 Kudos
MattDriscoll
Esri Contributor
If you can post all your code I can take a look.
0 Kudos
BenFousek
Deactivated User
Here: https://github.com/btfou/cutthroat-trout

When you can; no rush. Thanks
0 Kudos
MattDriscoll
Esri Contributor
Hey Ben,

Looks like it's not working right because you're using require() instead of define() in the custom module. Change it to define() and it should work.
0 Kudos
BenFousek
Deactivated User
Thanks Matt!
0 Kudos