
Description
Subject of the issue
gridstack.all.js:176 Uncaught Error: cannot call methods on draggable prior to initialization; attempted to call method 'destroy'
at Function.error (gridstack.all.js:176)
at HTMLDivElement. (gridstack.all.js:6010)
at Function.each (gridstack.all.js:197)
at E.fn.init.each (gridstack.all.js:126)
at E.fn.init.b.fn. [as draggable] (gridstack.all.js:6002)
at e.draggable (gridstack.all.js:8550)
at o. (gridstack.all.js:5476)
at Array.forEach ()
at o.removeAll (gridstack.all.js:5473)
at ?login:242
Your environment
gridstack.js 1.1.1
Chrome version 80.0.3987.163 (Official Build) (64-bit)
Steps to reproduce
Create two grids, add stuff to first grid then add stuff to second grid, remove one by one each widget from the second grid then call grid.removeAll() on that second grid, you will get this error as the nodes for that grid is not empty yet the grid it's self has no widgets.
Expected behaviour
What should happen is a check to see if the grid being cleared has remaining elements inside of it and whether or not the engine nodes list matches up or not, if not then empty the engine.nodes array so to match the actual status of the grid element.
Actual behaviour
The second grid is empty after calling grid.removeWidget on each widget but the engine.nodes of the grid in question still contains a node which causes the error to fire.
My solution is to check if the grid is empty using jquery, if it is empty and the grid.engine.nodes isn't empty I empty it using array splice which resolves the issue.
if(grids[1].engine.nodes.length>0 && $(bgGrid.el).is(':empty')) grids[1].engine.nodes.splice(0, grids[1].engine.nodes.length);
This solution is good as even if this is fixed in the future it won't break peoples projects.
It may be important to note in my testing that the main grid (grids[0]) doesn't have this issue, only additional grids which I find a bit odd.