Created
May 20, 2018 08:18
-
-
Save nothingrealhappen/03af3bde27f21b9f18b8b8133fc0a85e to your computer and use it in GitHub Desktop.
pageScript for turbolinks version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment