Skip to content

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]();
}
});

tormjens added 3 commits June 20, 2018 14:08
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
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