Reputation: 111
I have created a widget in jquery and calling it from cshtml. On first call widget is created successfully and data is shown properly. But when the widget is called 2nd time it does not hit the "_create" function of widget. Why is it so? Do I need to destroy the widget prior to create it again or should I initialize it first and then create it separately. If yes then please tell me how to do so?
This is my widget sample code.
$.widget("namespace.widgetname", {
//These are default options
options: {
Name: ""
},
//Triggers when widget called
_create: function () {
console.log($(this.element));
},
_destroy: function () {
this.element.remove();
}
})
widget is being called on button click like this.
function Preview() {
$("#targetdiv").widgetname({
Name: $('#txtAdminNameLeader').val()
});
}
So, in short, multiple calls are not triggering on widget call.
ANY HELP WILL BE HIGHLY APPRECIATED as I am stuck here for 3 days :( Thanks
Upvotes: 1
Views: 580
Reputation: 787
you dont need to use _destory function to destroy the widget. you need to call "destroy" explicitly like:
$("#targetdiv").widgetname("destroy");
then you can call widget again and it will most definitely hit the _create.
$("#targetdiv").widgetname({
Name: $('#txtAdminNameLeader').val()
});
also you can do it in single line as follows:
$("#targetdiv").widgetname({
Name: $('#txtAdminNameLeader').val()
}).widgetname("destroy");
this destory will only destroy the memory object. do whatever you want to do in _create of widget. hope that your problem will be solved. :)
Upvotes: 2