English 中文(简体)
Is there anything inherently dangerous about instantiating a dojo class like this?
原标题:

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?

最佳回答

At least two things can go wrong:

  • Your code assumes that a dynamically loaded module is loaded synchronously with dojo.require(). It is true only for the default loader. The XD loader will load things asynchronously breaking your logic.
  • An object is instantiated and its properties copied using dojo.mixin(), which by necessity will flatten it. It means:
    • It may override some internals (you preserve declaredClass, but there may be others).
    • OOP helpers (like this.inherited()) will be broken for copied methods.

But if these restrictions fit your use case, you should be fine.

It is hard to suggest improvements because it is not clear what you are trying to achieve. If you want to add flat mixins to an object, the only thing you need to make sure that the objects are truly flat.

Minor improvements to your code:

  • declaredClass is defined on object s prototype, not on object itself ⇒ you don t need to preserve it. Just delete it from the object itself:

    //var declaredClassBackup = this.declaredClass;  // backup the "declaredClass"
    // no need
    // the rest of your code
    ...
    /*
     * Re-set the declaredClass name back to that of this class.
     */
    //this.declaredClass = declaredClassBackup;
    // no need
    delete this.declaredClass;
    
  • Instead of dojo.mixin() you can use dojo.safeMixin(), which skips constructor and decorate methods. This method is available since Dojo 1.4 (including the current trunk).

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签