Davio
Davio

Reputation: 4737

Dojo DestroyRecursive Error on Widget

I'm working on converting Dojo from 1.6 to 1.9 and having some problems.

Before I reinstantiate a Dojo Widget, I'm attempting to destroy it with this library function:

function destroyWidget(idOrNode) {
    require(["dijit/registry"], function(registry) {
        var widget;

        if (typeof idOrNode === "string") {
            widget = registry.byId(idOrNode);
        }

        logDebug(widget);
        if (widget !== undefined && widget !== null) {
            widget.destroyRecursive();
        }

    });
}

I can pass an ID-string or an actual node to this function and let it destroy it for me.

I call it like this:

destroyWidget(myGrid);
myGrid = new EnhancedGrid({
              id: 'myGrid',
              store: myStore,
              structure: layout,
              plugins: {
                filter: true,
                pagination: {sizeSwitch: false},
                printer: true
              }
            },
            document.createElement('div'));
          dom.byId("divForGrid").appendChild(myGrid.domNode);
          myGrid.startup();

My "logDebug" shows the node as being an actual widget, but the following error is thrown: "TypeError: Cannot call method 'destroy' of null

So, I guess some child of my widget has already been destroyed. Can this be because of some sync issues? As in that the node trying to be destroyed has been reinstantiated already?

What am I doing wrong?

Upvotes: 0

Views: 2134

Answers (1)

Mithlesh Kumar
Mithlesh Kumar

Reputation: 758

You are passing parameter myGrid before assign any values to it. If you dry run your code you will see that first you call destroy function then create grid and assign it to myGrid.

Suppose we run code first time then, on 1st step destroy function will call and search for myGrid node but no value is assign to it as a result it will show error.

To fix this you may add an condition on destroy function as follow:

if(registry.byId("divForGrid")){
    destroyWidget(myGrid);
}

I hope this will help you.

Upvotes: 1

Related Questions