views:

102

answers:

1

I need to be able to instantiate an object of a class in Dojo at runtime and mix it into another object (kind of like specifying an extends or an implements in Java, but at runtime). and I came up with the following solution:

var declaredClassBackup = this.declaredClass;  // backup the "declaredClass" 

var mixinObject = null;
try {
    dojo.require(kwArgs.mixinClassName);

    /*
     * Eval the mixinClassName variable to get the Function reference, 
     * then call it as a constructor with our mixinSettings
     */
    mixinObject = new (eval(kwArgs.mixinClassName))(kwArgs.mixinSettings);
} catch (e){
    if(console){
        console.error("%s could not be loaded as a mixin.", 
                kwArgs.mixinClassName);
    }
    mixinObject = new package.path.DefaultMixin(kwArgs.mixinSettings);
}
dojo.mixin(this, mixinObject);

/*
 * Re-set the declaredClass name back to that of this class.
 */
this.declaredClass = declaredClassBackup;

What could go wrong with this type of code, if anything? (How would you make it more robust?) Also, is there something I could've just missed in dojo that would've done this for me more gracefully?

+1  A: 
Eugene Lazutkin