-
-
Notifications
You must be signed in to change notification settings - Fork 342
Description
Description
Current version of IAS binds as soon as the javascript is executed. This complicates listening for events that start on bind, because attaching listeners happen after the event is already dispatched (see example below). Some events that are impacted by this: prefill, next, binded and probably more.
bind is NOT impacted at the moment because is has a special case to trigger it after the fact, which you can see here:
infinite-ajax-scroll/src/infinite-ajax-scroll.js
Lines 269 to 271 in 0f5a57a
| if (event === Events.BINDED && this.binded) { | |
| callback.bind(this)(); | |
| } |
Example
Take this example:
1 let ias = new InfiniteAjaxScroll('.blocks', {
2 item: '.blocks__block',
3 next: '.pager__next',
4 trigger: '.trigger',
5 prefill: true,
6 });
7
8 ias.on('prefill', function() { /* do something */ });Here we are listening for the prefill event, but because the javascript is exectured right away, when the line 8 is executed the prefill event is already dispatched. So attaching the listener is happening after the fact.
Alternatives
An alternative solution would be to allow registration of listeners through an option. For example:
1 let ias = new InfiniteAjaxScroll('.blocks', {
2 item: '.blocks__block',
3 next: '.pager__next',
4 trigger: '.trigger',
5 prefill: true,
6 listeners: {
7 prefill: function() { /* do something */ },
8 }
9 });Another alternative would be to handle it in as similar way as the bind event. But I think that would cause more problems then that it solves.