Dipro Sen
Dipro Sen

Reputation: 4670

Dijit widget constructor throws "calling chained constructor" error

I've a Base class that inherits from [_WidgetBase, _TemplatedMixin]. Base is properly working. Now I inherit this Base in another class which is not working

define([
   "dojo/_base/declare", "dojo/parser", ...
], function(declare, parser, ...){
    return declare("mc.widgets.Base", [_WidgetBase, _TemplatedMixin], {
        templateString: 
            '<div class="mc-note-base">'+
            '</div>',
        constructor: function(argv){
            var self = this.inherited(arguments);
            return self;
        },
        data: function(){

        },
        postCreate: function(){
            ...
        }
    })
});

Derived class

define([
    "dojo/_base/declare", "mc/base/path", "mc/widgets/Base"
], function(declare, path, Base){
    return declare("mc.widgets.Derived", [Base], {});
})

The Derived Class throws

Error: declare mc.widgets.Derived: calling chained constructor with inherited

Upvotes: 3

Views: 2000

Answers (1)

Default
Default

Reputation: 16488

This is happening because the constructor portion of a Widget's lifecycle is handled with a special chaining mechanism, designed for more flexible Widget creation. You can read here for more information, but the part that applies to your situation says:

Superclass constructors are always called automatically, and always before the subclass constructor. This convention reduces boilerplate in 90% of cases. If it doesn’t fit your needs see Manual Constructor Chaining below. For all other methods, use this.inherited(arguments) to call the superclass method of the same name.

If you simply remove the this.inherited(arguments) call from your Widget's constructor method, your problem will be resolved. Here is a simple jsfiddle that mimics your Widget set up and demonstrates the solution.

Upvotes: 4

Related Questions