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

Update events listeners for page specific scripts #1667

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
25 changes: 19 additions & 6 deletions Guide/architecture.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,26 @@ Your global, non-page specific, JavaScript code can be placed in `app.js`.
E.g. the `app.js` could look like this:

```javascript
$(function () {
initNavbarEffects();
});
$(document).on('ready turbolinks:load', function () {
// This is called on the first page load *and* also when the page is changed by turbolinks.

function initNavbarEffects() {
// ...
}
// We make sure also the bind our events only once, so if a user navigates to
// another page and back, we won't try to bind the same event twice.
// Init sortable.
document.querySelectorAll('.js-my-selector').forEach(function (elem) {

// Check if the element is already initialized.
if (Boolean(elem.jsMySelectorInitialized) === false) {

// Bind event handlers here.
// ...

// Mark the element as initialized, so we won't bind the event handler twice.
elem.jsMySelectorInitialized = true;
}
});

});
```

In your `Web.View.Layout` just import the `app.js`:
Expand Down
12 changes: 6 additions & 6 deletions Guide/view.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,21 @@ Preloading with InstantClick on hover will only happen with links that

(So putting an anchor on a link, or explicitly setting the `data-turbolinks-preload` attribute to `false`, will let you selectively turn off preloading for that link.)

We provide two custom events

- `ihp:load` that will trigger when `DOMContentLoaded` or `turbolinks:load`
- `ihp:unload` that will fire on `beforeunload` and before [morphdom patches the page](#TurboLinks)
Triggered events:

```javascript
document.addEventListener('ihp:load', () => {
$(document).on('ready turbolinks:load', () => {
console.log('Page Loaded');
});

document.addEventListener('ihp:unload', () => {
$(document).on('beforeunload', () => {
console.log('Page Unloaded');
});
```

Note that when using jQuery there's a some difference from using `document.addEventListener`, which you can read about [here](https://api.jquery.com/ready/).
Notably the part about "If the DOM becomes ready and the browser fires `DOMContentLoaded` before the code calls `.ready( handler )`, the function handler will still be executed."

## JSON

Views that are rendered by calling the [`render`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Render.html#v:render) function can also respond with JSON.
Expand Down
7 changes: 0 additions & 7 deletions lib/IHP/static/helpers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
var ihpLoadEvent = new Event('ihp:load');
Copy link
Member

Choose a reason for hiding this comment

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

This change is a BC break, not sure whether that's worth it. ihpLoadEvent is also used somewhere in the file, so we'd need to remove the usages as well in case we want to delete these variables

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change is a BC break, not sure whether that's worth it

In my tests I saw that ihp:load was not working as expected. So on the one hand it's BC break, but on the other - people using it might think it's working. Should we ask on Slack who's using ihp:load, see if we get people to answer?

Copy link
Member

Choose a reason for hiding this comment

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

I just checked and saw we're ourselves using this in several projects:

image

e.g. like this:

image

So let's keep this event here. But I'm fine with removing the outdated documentation on this and replacing it

var ihpUnloadEvent = new Event('ihp:unload');

window.addEventListener('beforeunload', function () {
document.dispatchEvent(ihpUnloadEvent);
});

document.addEventListener('DOMContentLoaded', function () {
initDelete();
initDisableButtonsOnSubmit();
Expand Down