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

Dispatch event when firing routes #2080

Merged
merged 3 commits into from
Nov 16, 2018
Merged

Dispatch event when firing routes #2080

merged 3 commits into from
Nov 16, 2018

Conversation

tormjens
Copy link
Contributor

@tormjens tormjens commented Jun 20, 2018

It is especially neat when importing the scripts from the parent theme in a child theme. In one of my themes I do it like this:

// routes.js
export default {
    home: {
      init() {
        console.log('Home route fired.')
      }
    }
}
// main.js
/**
* Parent Themes Script
*/
import '../../../../core-theme/resources/assets/scripts/main';

/**
* Internal modules
*/
import routes from './routes'

/**
* Hook in to the parent themes router
*/
document.addEventListener('routed', e => {
let route = e.detail.route,
  fn = e.detail.fn;

if (routes[route] && typeof routes[route][fn] === 'function') {
  routes[route][fn]();
}
});

It is especially neat when importing the scripts from the parent theme in a child theme. In one of my themes I do it like this:

```js
// routes.js
import home from './routes/home'

export default {
    home: {
      init() {
        console.log('Home route fired.')
      }
    }
}
```

  ```js
// main.js
/**
 * Parent Themes Script
 */
import '../../../../core-theme/resources/assets/scripts/main';

/**
 * Internal modules
 */
import routes from './routes'

/**
 * Hook in to the parent themes router
 */
document.addEventListener('routed', e => {
  let route = e.detail.route,
    fn = e.detail.fn;

  if (routes[route] && typeof routes[route][fn] === 'function') {
    routes[route][fn]();
  }
});
```
@retlehs retlehs merged commit 524d1e2 into roots:master Nov 16, 2018
@retlehs
Copy link
Sponsor Member

retlehs commented Nov 16, 2018

thanks and sorry for the wait!

devs needing to support ie11 will need a polyfill: https://github.com/kumarharsh/custom-event-polyfill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants