Skip to content
This repository has been archived by the owner on Dec 30, 2018. It is now read-only.

fix(directive): check for existing elements before appending #167

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

JeffreyATW
Copy link
Contributor

Don't append elements that Masonry has already added to its items.

This fixes the situation in which a static element has already been added to Masonry, but then the masonry directive attempts to add a new masonry-brick into Masonry's item list.

Don't append elements that Masonry has already added to its items.
@JeffreyATW JeffreyATW mentioned this pull request Feb 18, 2016
@@ -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.masonry('getItemElements').indexOf(element.get(0)) === -1) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be O(n^2) when adding a list of n new elements, this seems like a very dangerous default. Is there any other way to address this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there's a more "Angular" way of doing this, but within initialization of the masonry directive, it could iterate once through every existing child element and run something like child.data('angularMasonryAdded', true). Then the above code could be changed to check if the element has that data attribute, avoiding per-element iteration. What do you think?

…as static

Using jQuery's data functionality, we can mark static elements upon initialization.
Avoid using possibly non-existent global $.
Angular-masonry supports dynamic elements by default; Be explicit in the specs that most cases involve dynamically repeating bricks
@JeffreyATW
Copy link
Contributor Author

@passy Fixed the specs to reflect the general use of ng-repeat, except in the case of static elements.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants