Question

This is a followup to this question.

So I have this pre AMD dojo code :

dojo.require(...);
dojo.provide("abc.def.foo");
som.var.iable = {A:1,B:2};
som.var.iable2 = {C: 3, D:som.var.iable.B}
dojo.declare("blah",[],{
     //code based on the above variables
});

For AMD, after reading this and the previous link, I am trying something like this

som.var.iable = {A:1,B:2};
som.var.iable2 = {C: 3, D:som.var.iable.B}

define([
    "dojo/_base/declare", 
], function(declare){
   return declare("abc.def.foo", null {               
   });
});

define([
    "dojo/_base/declare", 
], function(declare){
   som.var.iable = {A:1,B:2};
som.var.iable2 = {C: 3, D:som.var.iable.B}
   return declare("blah", null {
        //code based on the above variables
   });
});

Obviously this fails, as there is no object structure like som.var.iable. I can it, but my question is how did it work in the legacy code? and what would be the correct AMD equivalent?

Any help is greatly appreciated!

Was it helpful?

Solution

OK, here are my assumptions about what you're trying to do:

  1. You don't really need a global variable called some with a property var, that's just a way to organize stuff
  2. You want three modules, some/var/iable, some/var/iable2, and blah. This means three files and three define() calls
  3. Neither som.var.iable nor som.var.iable2 are real inheritable classes, they're just plain old objects... so only blah needs to use declare()

Thus you should create a file som/var/iable.js, which is a provides a plain object:

define([
],
function(){
    return {A:1,B,2}
});

And another called som/var/iable2.js, which is a module that provides a plain object:

define([
    "som/var/iable",
],
function(iable){
    return {C: 3, D:iable.B}
});

And then your third module blah.js that provides a Class-object:

define([
    "dojo/_base/declare",
    "som/var/iable2"
],
function(declare,iable2){
    var parentClasses = [];
    var clazz =  declare(parentClasses, {
        constructor : function(){
            // No need for this.inherited(arguments) in this particular case          
            alert("Welcome to the constructor. Did you know that iable2.D is "+iable2.D+"?");   
        },

    });
    return clazz;
});

I haven't tested all this, but to kick it off in a page you'd finally want to put:

require(["blah",dojo/domReady!"], function(blah){
    var b = new blah();
});

Dojo should take care of loading everything in-order so that you get an alert that says

Welcome to the constructor. Did you know that iable2.D is 2?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top