pageScript for turbolinks version
export const pageScript = (path, fn) => (...args) => {
$(document).on('turbolinks:load', () => {
const pageID = $('body').attr('id');
const map = {
string: () => pageID === path,
object: () => path.some(x => x === pageID)
};
const isAllow = map[typeof path]();
return isAllow ? fn(...args) : null;
});
};
// usage:
// homeIndex.js
const homeIndex = () => {
alert(`I'm home page specific logic`);
}
export default pageScript('home-index', homeIndex);