From b809bb743a4676efb11d818506ce5d33d3d976c3 Mon Sep 17 00:00:00 2001 From: Stephen Wilkinson Date: Tue, 14 Feb 2017 11:23:33 +0000 Subject: [PATCH 1/6] Added post-add event to collection js and updated docs --- Resources/doc/collection-helper.md | 15 +++++++++++++++ Resources/public/js/collections.js | 19 +++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Resources/doc/collection-helper.md b/Resources/doc/collection-helper.md index db705fe..d24bc48 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 was 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..44434c1 100644 --- a/Resources/public/js/collections.js +++ b/Resources/public/js/collections.js @@ -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); } else { this.$collection.append($row); + this.$collection.trigger(addedEvent); } return $row; From 6844949198b6fd3e72748d9d18feefeb48ee0781 Mon Sep 17 00:00:00 2001 From: Stephen Wilkinson Date: Thu, 16 Feb 2017 14:38:48 +0000 Subject: [PATCH 2/6] Fixed document typo and optimised event trigger logic --- Resources/doc/collection-helper.md | 2 +- Resources/public/js/collections.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Resources/doc/collection-helper.md b/Resources/doc/collection-helper.md index d24bc48..a50b7a0 100644 --- a/Resources/doc/collection-helper.md +++ b/Resources/doc/collection-helper.md @@ -168,7 +168,7 @@ Properties provided on the event object: 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. +- `$row`: the jQuery wrapped DOM elements that were added to the collection. ### infinite_collection_remove diff --git a/Resources/public/js/collections.js b/Resources/public/js/collections.js index 44434c1..d42efd2 100644 --- a/Resources/public/js/collections.js +++ b/Resources/public/js/collections.js @@ -101,12 +101,12 @@ if (!addEvent.isDefaultPrevented()) { if (event.insertBefore) { $row.insertBefore(event.insertBefore); - this.$collection.trigger(addedEvent); } else { this.$collection.append($row); - this.$collection.trigger(addedEvent); } + this.$collection.trigger(addedEvent); + return $row; } }, From f9a7ea524e44c0ea2d434c44a62a834b18bcc38e Mon Sep 17 00:00:00 2001 From: Stephen Wilkinson Date: Thu, 16 Feb 2017 14:54:07 +0000 Subject: [PATCH 3/6] Moved event creation before trigger --- Resources/public/js/collections.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Resources/public/js/collections.js b/Resources/public/js/collections.js index d42efd2..404c717 100644 --- a/Resources/public/js/collections.js +++ b/Resources/public/js/collections.js @@ -94,10 +94,6 @@ 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); @@ -105,6 +101,11 @@ this.$collection.append($row); } + var addedEvent = this._createEvent('infinite_collection_added'); + + addedEvent.$triggeredPrototype = $prototype; + addedEvent.$row = $row; + this.$collection.trigger(addedEvent); return $row; From 77c9063008632d2b88e578e519bdbb203a54c870 Mon Sep 17 00:00:00 2001 From: Stephen Wilkinson Date: Thu, 16 Feb 2017 15:00:05 +0000 Subject: [PATCH 4/6] Fixed error causing existing tests to fail --- Resources/public/js/collections.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Resources/public/js/collections.js b/Resources/public/js/collections.js index 404c717..1de0c6e 100644 --- a/Resources/public/js/collections.js +++ b/Resources/public/js/collections.js @@ -88,6 +88,7 @@ $row = $($.parseHTML(html, document, this.options.keepScripts)); var addEvent = this._createEvent('infinite_collection_add'); + addEvent.$triggeredPrototype = $prototype; addEvent.$row = $row; addEvent.insertBefore = null; @@ -95,8 +96,8 @@ this.$collection.trigger(addEvent); if (!addEvent.isDefaultPrevented()) { - if (event.insertBefore) { - $row.insertBefore(event.insertBefore); + if (addEvent.insertBefore) { + $row.insertBefore(addEvent.insertBefore); } else { this.$collection.append($row); } From e376974e43bb3c081dc59b18dcbc7ea3760bd05e Mon Sep 17 00:00:00 2001 From: Stephen Wilkinson Date: Thu, 16 Feb 2017 15:04:11 +0000 Subject: [PATCH 5/6] Added test for item added event --- Tests/Javascript/collection_tests.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/Javascript/collection_tests.js b/Tests/Javascript/collection_tests.js index a53f9ed..25e14a6 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(2); + + 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) { From 15436b0bd0f3197d5c7844ecbc1d69ae9556f3db Mon Sep 17 00:00:00 2001 From: Stephen Wilkinson Date: Thu, 16 Feb 2017 15:12:21 +0000 Subject: [PATCH 6/6] Expect one assertion for added event test --- Tests/Javascript/collection_tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Javascript/collection_tests.js b/Tests/Javascript/collection_tests.js index 25e14a6..4814ea2 100644 --- a/Tests/Javascript/collection_tests.js +++ b/Tests/Javascript/collection_tests.js @@ -162,7 +162,7 @@ }); test("Added Event", function () { - expect(2); + expect(1); var collection = setUpCollection('#markup .list-collection');