How to find dojo module module id

I have a set of global javascript page resources that I need to manage from different dojo classes. In older versions (dojo 1.6), I used the declaredClassclass instance property to determine which class was interested in accessing the shared resource and act accordingly.

Now that I use the AMD bootloader and convert my modules accordingly, this method no longer works. Actually, there seems to be no way to find out that the module identifier of the class instance is at runtime. I looked at the dojo 1.9 code dojo\_base\declare, but it seems that a unique identifier is assigned only if there is a common base class through the function c3mro.

Here are two examples of the seemingly counterintuitive behavior of functions declaredClasscompared to older dojo versions.

Case 1: two classes with a common base class

base.js

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

ClassA.js and ClassB.js

define(["dojo/_base/declare", "./_Base"], function(declare, _Base) {
    return declare([_Base], {
    });
});

Script test

require(["myapp/test/ClassA", "myapp/test/ClassB"], function (A, B) {
    var a = new A();
    var b = new B();

    console.log(a.declaredClass);
    console.log(b.declaredClass);
});

Output

uniqName_1
uniqName_1

If I change the test, so that ClassAthey ClassBdo not inherit from _Baseor use simple inheritance, passing in _Baseas one argument, and not in an array, then I get undefinedfor output.

ClassA.js and ClassB.js

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

or

define(["dojo/_base/declare", "./_Base"], function(declare, _Base) {
    return declare(_Base, {
    });
});

Test output

undefined
undefined

I can get around the problem by explicitly specifying a classNamefunction argument declare(), but this is clearly not recommended for writing portable modules.

So, is there a way to find out from the class instance which module identifier was used to instantiate this class?

UPDATE

, , _Base . , 1.7. , , , 1.7 AMD.

base.js

define(["dojo/_base/declare", "dojo/_base/kernel"], function (declare, kernel) {
    return declare(null, {
        activate: function(item) {
            // Get the set of current active items
            var activeItems = kernel.global.activeItems || {};

            // Disable anything that is currently active, unless it for the
            // current instance
            for (var key in activeItems) {
                if (key !== this.declaredClass) {
                    activeItems[key].deactivate();
                }
            }

            // Activate and save the current item
            item.activate();
            activeItems[this.declaredClass] = item;

            kernel.global.activeItems = activeItems;
        },

        deactivate: function(item) {
            var activeItems = kernel.global.activeItems || {};
            if (activeItems[this.declaredClass]) {
                item.deactivate();
                delete activeItems[this.declaredClass];
            }
        }      
    });
});
+3
1

, - declare(). , , , . , , declaredClass, - .

, Dojo , . , myPackage/ClassA, , myExtremelyCoolPackage/ClassA. , .

, AMD, . , , - , - . , declare(), , .

0

All Articles