From ac2908e1a2f0a3fd774f0ac26eca5e09d200a814 Mon Sep 17 00:00:00 2001 From: Davide Callegari Date: Fri, 15 Feb 2013 14:30:20 +0100 Subject: [PATCH] Added a destroy method on the Gridster class --- src/jquery.gridster.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/jquery.gridster.js b/src/jquery.gridster.js index 7cf383438..44c44fa27 100644 --- a/src/jquery.gridster.js +++ b/src/jquery.gridster.js @@ -106,8 +106,9 @@ this.$wrapper.addClass('ready'); this.draggable(); - $(window).bind( - 'resize', throttle($.proxy(this.recalculate_faux_grid, this), 200)); + this.resize_callback = throttle($.proxy(this.recalculate_faux_grid, this), 200); + + $(window).bind('resize', this.resize_callback); }; @@ -2520,6 +2521,25 @@ return this.generate_faux_grid(this.rows, this.cols); }; + /** + * Destroy this gridster by removing any sign of its presence, making it easy to avoid memory leaks + * + * @method destroy + * @return {undefined} + */ + fn.destroy = function(){ + // remove bound callback on window resize + $(window).unbind('resize', this.resize_callback); + + // TODO: remove draggable bindings + this.drag_api.destroy(); + + // lastly, remove gridster element + // this will additionally cause any data associated to this element to be removed, including this + // very gridster instance + this.$el.remove(); + }; + //jQuery adapter $.fn.gridster = function(options) {