@locks I have tried it in my build, but I could not get the jQuery Dialog working. I need to call many dialogs and load data inside it. Is there any working example on how to load the dialog then destroy it completely and load another dialog.
I use some jQuery libs in my Ember app. When I have to do some stuff with them, I create a specific view/component and I insert the jQuery functionality in the didInsertElement method this way:
didInsertElement: function () {
this._super();
Ember.run.scheduleOnce('afterRender', this, function () {
// some jQuery UI stuff
});
}
```
I hope it helps :smiley:
@Gorzas Thanks for your reply and it helped me a lot. I used a Component and added my initialisation and destroy of the jQuery component in disInsertElement.
Also one more doubt regarding use of Ember Component.
If I have to use many Components in my application, I have to include all the components in my application.
Is there any other way to include the component when ever required and destroy it when its usage is over. I mean to say DYNAMIC Components include and destroy it.
Equally as important as initializing, youâll want to be very proactive in tearing down the jQuery widget within the willDestroyElement hook to prevent nasty memory leaks and the component sticking around for the lifetime of your app.
@jasonmit Thanks for your reply. In my case I am in a route, and have a single component inside that route view.
Now I need to initialise my jQuery widgets (like Dialog) inside that Component and load some data inside the dialog. Then I need to close the dialog by completely destroying the dialog related events and bindings.
After that I will be in the same route and the Component is bound to the route. Now I need to again initialise another dialog inside the same Component.
When will willDestroyElement will be called after the Component is loaded inside my route.
Can I remove all my jQuery widget related bindings and destroy it over there.
Can I again initialise another dialog widget within the same Component without any memory leak.
You can call this.destroy() I think. willDestroyElement triggers when then element is removed for any reason (route transition is the most common, but you can listen to other events or make your own)
willDestroyElement gets called when the component is going to be removed from the DOM. â Called when the element of the view is going to be destroyed. Override this function to do any teardown that requires an element, like removing event listeners.
In my case my view is not going to be destroyed. I will be in the same view and I need to load different jQuery Dialog Widget inside the same Component.
Now how can I destroy all the events and binding related to the dialog widget when it is closed to avoid memory leak.
Keep in mind that âdidInsertElementâ only fires once, after the initial base template for the view has been inserted into the DOM, but not before the dynamic elements have been rendered. Thatâs why you need to use the run loop and the âafterRenderâ queue to select content that gets rendered inside {{mustaches}} or with {{#helpers}}{{/helpers}}.
You only need to use this._super() if youâre inheriting from a parent view/component that has an existing âdidInsertElementâ method.
Perhaps for some 3rd party libs, but certainly not for ember-specific stuff. If, so mega-bug somewhere. You shouldnât be calling super anywhere, but listening to initialization instead.
I see what youâre saying regarding _super. Listening to the event is a much safer pattern, and simple to implement.
Regarding the 3rd party libs, in this case it doesnât appear the run loop is necessary, but in many cases youâre dealing with content that is not yet rendered at the time the âdidInsertElementâ event occurs. I just wanted to clarify that point, as it tripped me up early on with Ember.