Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post-add event to collection js #64

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Resources/doc/collection-helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ Properties provided on the event object:
- `insertBefore`: if set by an event listener, the row will be inserted before this dom
element.

### infinite_collection_added

This event is fired when a new item has been added to a collection. It allows
functionality to work with the post-add dom without the need to add dom mutate observers.

Properties provided on the event object:

- `collection`: The window.infinite.Collection instance
- `$triggeredPrototype`: this is the dom element that triggered the adding of an item to
the collection. In the case of a normal collection type, the
prototype will be the add button. In the case of the
Polycollection, the prototype will be one of the prototype
buttons.
- `$row`: the jQuery wrapped DOM elements that was added to the collection.
Copy link
Contributor

Choose a reason for hiding this comment

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

grammar: dom elements that were added


### infinite_collection_remove

This event is fired before a row is to be removed from the DOM. This event does not fire
Expand Down
19 changes: 13 additions & 6 deletions Resources/public/js/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,24 @@
var html = this._getPrototypeHtml($prototype, this.internalCount++),
$row = $($.parseHTML(html, document, this.options.keepScripts));

var event = this._createEvent('infinite_collection_add');
event.$triggeredPrototype = $prototype;
event.$row = $row;
event.insertBefore = null;
this.$collection.trigger(event);
var addEvent = this._createEvent('infinite_collection_add');
addEvent.$triggeredPrototype = $prototype;
addEvent.$row = $row;
addEvent.insertBefore = null;

if (!event.isDefaultPrevented()) {
this.$collection.trigger(addEvent);

var addedEvent = this._createEvent('infinite_collection_added');
addedEvent.$triggeredPrototype = $prototype;
addedEvent.$row = $row;

if (!addEvent.isDefaultPrevented()) {
if (event.insertBefore) {
$row.insertBefore(event.insertBefore);
this.$collection.trigger(addedEvent);
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be moved below the if and before the return to avoid repeating the same call in both branches.

You should also only create the event before it is about to be fired, inside the isDefaultPrevented() check.

} else {
this.$collection.append($row);
this.$collection.trigger(addedEvent);
}

return $row;
Expand Down