diff --git a/src/angular-masonry.js b/src/angular-masonry.js index 1ce8977..e82dc14 100755 --- a/src/angular-masonry.js +++ b/src/angular-masonry.js @@ -65,7 +65,10 @@ // Keep track of added elements. bricks[id] = true; defaultLoaded(element); - $element.masonry(method, element, true); + // Don't add element to Masonry if it already has it. + if (element.data('angularMasonryStatic') !== true) { + $element.masonry(method, element, true); + } } } @@ -131,6 +134,9 @@ columnWidth: parseInt(attrs.columnWidth, 10) || attrs.columnWidth }, attrOptions || {}); element.masonry(options); + element.children().each(function () { + angular.element(this).data('angularMasonryStatic', true); + }); scope.masonryContainer = element[0]; var loadImages = scope.$eval(attrs.loadImages); ctrl.loadImages = loadImages !== false; diff --git a/test/spec/directive.coffee b/test/spec/directive.coffee index a89fd1e..0d867fc 100644 --- a/test/spec/directive.coffee +++ b/test/spec/directive.coffee @@ -132,12 +132,14 @@ describe 'angular-masonry', -> ) it 'should register an element in the parent controller', inject(($compile) => + @scope.bricks = [1] element = angular.element ''' -
+
''' element = $compile(element)(@scope) + @scope.$digest() # Needed for initial ng-repeat expect(@addBrick).toHaveBeenCalledOnce() ) @@ -161,18 +163,18 @@ describe 'angular-masonry', -> ) describe 'masonry-brick internals', => - beforeEach -> + beforeEach => $.fn.imagesLoaded = (cb) -> cb() + @scope.bricks = [1] afterEach -> delete $.fn.imagesLoaded it 'should append three elements to the controller', inject(($compile) => + @scope.bricks = [1, 2, 3] element = angular.element ''' -
-
-
+
''' element = $compile(element)(@scope) @@ -184,7 +186,7 @@ describe 'angular-masonry', -> it 'should prepend elements when specified by attribute', inject(($compile) => element = angular.element ''' -
+
''' element = $compile(element)(@scope) @@ -196,7 +198,7 @@ describe 'angular-masonry', -> it 'should append before imagesLoaded when preserve-order is set', inject(($compile) => element = angular.element ''' -
+
''' imagesLoadedCb = undefined @@ -209,7 +211,7 @@ describe 'angular-masonry', -> it 'should call layout after imagesLoaded when preserve-order is set', inject(($compile, $timeout) => element = angular.element ''' -
+
''' imagesLoadedCb = undefined @@ -225,7 +227,7 @@ describe 'angular-masonry', -> it 'should append before imagesLoaded when load-images is set to "false"', inject(($compile) => element = angular.element ''' -
+
''' imagesLoadedCb = undefined @@ -238,7 +240,7 @@ describe 'angular-masonry', -> it 'should call layout before imagesLoaded when load-images is set to "false"', inject(($compile, $timeout) => element = angular.element ''' -
+
''' imagesLoadedCb = undefined @@ -248,3 +250,14 @@ describe 'angular-masonry', -> $timeout.flush() expect($.fn.masonry.calledWith('layout', sinon.match.any, sinon.match.any)).toBe(true) ) + + it 'should not append if masonry already has the element', inject(($compile) => + element = angular.element ''' + +
+
+ ''' + element = $compile(element)(@scope) + @scope.$digest() + expect($.fn.masonry.calledWith('appended', sinon.match.any, sinon.match.any)).toBe(false) + )