Amit Dube
Amit Dube

Reputation: 1027

startup is not getting called for Dojo Custom Widget

I created a Custom Widget in Dojo

     return declare("DrawTools", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
        templateString: template,
        layers: [],
        constructor: function (featureLayerArr) {

        },
        postCreate: function () {

        },
        startup: function () {
            var menu = new DropDownMenu({ style: "display: none;" });
            var menuItem1 = new MenuItem({
                label: "Save",
                iconClass: "dijitEditorIcon dijitEditorIconSave",
                onClick: function () { alert('save'); }
            });
            menu.addChild(menuItem1);

            var menuItem2 = new MenuItem({
                label: "Cut",
                iconClass: "dijitEditorIcon dijitEditorIconCut",
                onClick: function () { alert('cut'); }
            });
            menu.addChild(menuItem2);
            menu.startup();
            var button = new DropDownButton({
                label: "hello!",
                name: "programmatic2",
                dropDown: menu,
                id: "progButton"
            }, this.drawToolsMenuNode).startup();
        },
        startMenu: function () {

        }
    });

Wdiget template is as follows

<div>
  <div data-dojo-attach-point="drawToolsMenuNode"></div>
</div>

I am instantiating Widget in another Custom Widget as follows

var drawTools = new DrawTools(this.allLayersArr);
drawTools.placeAt(this.drawToolsNode);
drawTools.startMenu();

startup method for DrawTools widget is not getting called.

Need help in this regard.

Upvotes: 0

Views: 537

Answers (2)

Mai
Mai

Reputation: 301

The alternative solution would be moving widget instantiation logic from ::startup() to ::postCreate(), since ::postCreate() will be called for sure.

Upvotes: 0

bajji
bajji

Reputation: 1291

Offical definition from dojo

startup(): Probably the second-most important method in the Dijit lifecycle is the startup method. This method is designed to handle processing after any DOM fragments have been actually added to the document; it is not fired until after any potential child widgets have been created and started as well. This is especially useful for composite widgets and layout widgets.

When instantiating a widget programmatically, always call the widget's startup() method after placing it in the document. It's a common error to create widgets programmatically and then forget to call startup, leaving you scratching your head as to why your widget isn't showing up properly.

So as Kirill mentioned, you need to call the startup method.

Upvotes: 3

Related Questions