Search code examples
jquerypackery

How to reset Packery Instance and reinitializing


How to reset the packery?

If i use packery('destroy') it resets the structure but on reinitializing it lost the order instance always give same index number....

I tired reinitializing the entire elements but double binding make life more difficult.

var $slidesElem = $('ul.ordering-tool-list'),
             slidesHeight = $slidesElem.find('li').outerHeight(),
             slidesWidth = $slidesElem.find('li').outerWidth();
             if(typeof $scope.pckry !=='undefined'){
                 $scope.initAside.onlyDestroy();
             }
        //Packery Call
        this.initialize =function(execute){
             $scope.pckry = $slidesElem.packery({
                            'itemSelector':".handleMe",
                            'rowHeight': slidesHeight,
                            'columnWidth': slidesWidth
                        });
            var itemElems = $scope.pckry.packery('getItemElements');
            $scope.initialItem = itemElems;

            for ( var i=0, len = itemElems.length; i < len; i++ ) {
                // make element draggable with Draggabilly
                var draggie = new Draggabilly( itemElems[i],{
                    axis: 'y',
                    containment:$slidesElem,
                    grid: [ 0, 24 ]
                });
                // bind Draggabilly events to Packery
                $scope.pckry.packery('bindDraggabillyEvents', draggie);
              }

            $scope.pckry.packery('on', 'dragItemPositioned',this.orderItems);
            $scope.pckry.packery('on', 'layoutComplete', this.orderItems );

        }
       //Set the orderof Items
        this.orderItems = function() {
            var $itemElems = $scope.pckry.packery('getItemElements');
             for (var i=0; i< $itemElems.length; i++) {
                var indexToPut = $($itemElems[i]).attr("tabindex");
              $scope.itemPositioned[i] = indexToPut;
              $scope.profileContent[indexToPut].id = i;
              $($itemElems[i]).attr('data-module-index', i);
            }
            $scope.$apply();
        };
        //reset The Order
        this.resetOrder = function(){
             var $itemElems = $scope.initialItem;
             for (var i=0; i< $itemElems.length; i++) {
              $scope.itemPositioned[i] = $($itemElems[i]).attr("tabindex");
              $($itemElems[i]).attr('data-module-index', i); 
              $scope.profileContent[i].id = i;
            }
        };
        //Destroy Event for reset
        this.destroy = function(){
            //$scope.pckry.packery('destroy');
            //$scope.pckry.packery();
            //$scope.pckry = null;
            //this.initialize(false);
            this.resetOrder();
            //this.initialize(false);

        };

Solution

  • Found the solution use "reload"

    $scope.pckry.packery('reloadItems');