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 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
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
14 changes: 8 additions & 6 deletions Guide/view.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,23 @@ 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');
});
```

There's also a custom event `ihp:unload` that will fire on `beforeunload` and before [morphdom patches the page](#TurboLinks)

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