Created
September 10, 2014 19:41
-
-
Save jGRUBBS/ed68799fca45a824f1ae to your computer and use it in GitHub Desktop.
initialize scripts pragmatically
This file contains 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
%div{ data: { init: 'somescript' } } |
This file contains 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
initialize = | |
inited: [] | |
loaded: [] | |
objects: -> | |
$('[data-init]').not('[data-load]').each (index, element) => | |
if $.inArray($(element).data('init'), initialize.inited) == -1 | |
initialize.inited.push($(element).data('init')) | |
window[$(element).data('init')].init() | |
load_objects: -> | |
$('[data-load]').each (index, element) => | |
if $.inArray($(element).data('load'), initialize.loaded) == -1 | |
initialize.inited.push($(element).data('load')) | |
window[$(element).data('load')].init() | |
jQuery -> | |
initialize.objects() | |
$(window).load -> | |
initialize.load_objects() |
This file contains 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
window.somescript = | |
init: -> | |
alert 'this is called when the data-init attribute is loaded on a certain page' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment