I’m using a bunch of plugins in a Jquery Mobile site and am looking for the best way to “clean up” once I’m leaving a page, which stays in the DOM.
Question is, should I try to unbind/off/undelegate plugin bindings and re-initialize when the page is pulled back into view, or should I leave everything running “in the background” while the user is on another page?
Either way, what would be the best way to do this, if I’m setting up plugins like so:
// example using flexslider plugin
$('div:jqmData(role="page").basePage').on('pagecreate.flexslider', function() {
// make sure this fires only once when pagecreates fires more often
if ( $('.flexslider').length > 0 && $('.flexslider').jqmData('bound') != true ) {
// lock
$('.flexslider').jqmData('bound',true);
$('.flexslider').flexslider({
animation: "slide",
controlsContainer: $(".flex-container")
});
}
});
Question:
Say this is my Jquery Mobile anchor-page (always stays in the DOM), should I off when the user leaves the page and re-init on re-load or keep the slider running in the background?
Whats the best practive here?
When you leave a page that was brought into the DOM via AJAX, jQuery Mobile will automatically remove the page from the DOM, including it’s descendants and all of their event-handlers/data.
Source: http://jquerymobile.com/demos/1.1.0/docs/pages/page-cache.html
.
Internally jQuery Mobile uses
.remove():Source: http://api.jquery.com/remove/