diff --git a/Resources/doc/collection-helper.md b/Resources/doc/collection-helper.md index db705fe..a50b7a0 100644 --- a/Resources/doc/collection-helper.md +++ b/Resources/doc/collection-helper.md @@ -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 diff --git a/Resources/public/js/collections.js b/Resources/public/js/collections.js index 03ad5ad..1de0c6e 100644 --- a/Resources/public/js/collections.js +++ b/Resources/public/js/collections.js @@ -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; } }, diff --git a/Tests/Javascript/collection_tests.js b/Tests/Javascript/collection_tests.js index a53f9ed..4814ea2 100644 --- a/Tests/Javascript/collection_tests.js +++ b/Tests/Javascript/collection_tests.js @@ -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) {