Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 were added to the collection.

### infinite_collection_remove

This event is fired before a row is to be removed from the DOM. This event does not fire
Expand Down
25 changes: 17 additions & 8 deletions Resources/public/js/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,28 @@
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');

if (!event.isDefaultPrevented()) {
if (event.insertBefore) {
$row.insertBefore(event.insertBefore);
addEvent.$triggeredPrototype = $prototype;
addEvent.$row = $row;
addEvent.insertBefore = null;

this.$collection.trigger(addEvent);

if (!addEvent.isDefaultPrevented()) {
if (addEvent.insertBefore) {
$row.insertBefore(addEvent.insertBefore);
} else {
this.$collection.append($row);
}

var addedEvent = this._createEvent('infinite_collection_added');

addedEvent.$triggeredPrototype = $prototype;
addedEvent.$row = $row;

this.$collection.trigger(addedEvent);

return $row;
}
},
Expand Down
12 changes: 12 additions & 0 deletions Tests/Javascript/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@
'Add item added another prototype to the collection');
});

test("Added Event", function () {
expect(1);

var collection = setUpCollection('#markup .list-collection');

collection.$collection.on('infinite_collection_added', function (e) {
ok(true, 'Added event fired');
});

collection.$prototypes.click();
});

test("Add Event Prevents adding", function () {
var collection = setUpCollection('#markup .list-collection');
collection.$collection.on('infinite_collection_add', function (e) {
Expand Down