liyaodong
5/20/2018 - 8:18 AM

pageScript for turbolinks version

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);