raoel
raoel

Reputation: 13

dojo.mixin not working as expected

I think I am using dojo.mixin not as required...

I have the following code:

dojo.declare("A",null,{
    _configuration: {},
    constructor: function(configuration)
    {
        if(this._configuration.something === 2)
        {
            // we should never come here; the constructor is only called on a new instance which should still have an empty _somethingPrivate
            // because the class declaration says so
            console.log("why does this happen?");
        }

        // merge empty configuration 
        dojo.mixin(this._configuration, configuration);
    }
});

var myInstance = new A({ something: 2 });
var myInstance = new A({});

As far as I understand, you can use dojo.mixin to merge objects. I try to merge a default configuration object with given parameters (in an object), but the console output is "why does this happen?" so the parameters from previous object are merged into a new object.

Can anyone shed some light on this?

By the way: dojo version 1.6 (we cannot upgrade yet)

Upvotes: 0

Views: 179

Answers (1)

Craig Swing
Craig Swing

Reputation: 8162

Because you define the configuration as _configuration: {}, it is shared across all instances of the widget. So when you initialize the second instance, it sees the config from the first instance. See http://dojotoolkit.org/reference-guide/1.6/dojo/declare.html for more details.

_defaultConfig: {},

_configuration: null,

constructor: function(config) {
    // clone to use a separate object.
    this._configuration = dojo.mixin(dojo.clone(this._defaultConfig), config);
}

Upvotes: 3

Related Questions