sjs
sjs

Reputation: 472

How to destroy widget while preserving the root node to which it was attached?

Is there a way to destroy a widget but preserve the dom node to which it was attached? For example I have a div

<body>
    <div id="app">
    </div>
</body>

When i attach the widget like this

 new someWidget({}, 'app');

to this node by Id I get something like

<body>
    <div id="app" widgetid="app">
    </div>
</body>

Then when i call destroy on the widget like this

var mywidget = registry.byId("app");
mywidget.destroy();

I end up with

<body>
</body>

The entire div to which the widget was attached disappears. But what i really want is to 'detach' then destroy the widget and keep the original div to which it was attached.

Is there a way?

Upvotes: 0

Views: 288

Answers (1)

Himanshu
Himanshu

Reputation: 1002

There are two ways to go about this. First is to use a data-dojo-attach-point and place the widget node using domConstruct.place. This will append the widget node to the target thus making the widget a child of the target node. Later, when you will destroy the widget, the main node in which you placed the widget will not be destroyed.

<body>
<div data-dojo-attach-point="app"></div>
</body>


var myWidget= new someWidget();
domConstruct.place(myWidget.domNode, this.app);

Second is to use myWidget.destroy(true). See the __WidgetBase destroy() documentation here. The argument passed refers to the preserveDom attribute and is false by default. It it is true, then the original DOM structure will be preserved. Quoting from the source page:

If true, this method will leave the original DOM structure alone. Note: This will not yet work with _TemplatedMixin widgets

Upvotes: 1

Related Questions