Skip to content

Instantly share code, notes, and snippets.

@rewritten
Created September 16, 2016 17:00
Show Gist options
  • Save rewritten/07c4d1716bbf5cc7baa595258479d4be to your computer and use it in GitHub Desktop.
Save rewritten/07c4d1716bbf5cc7baa595258479d4be to your computer and use it in GitHub Desktop.
Example wrapper + functio + document:ready + turbolinks:load
/////////////////////////////////////////////////////////
// Normal wrapper, using globals inside of it, but
// local variables and functions are not leaking out.
(function () {
// function to execute on load
var circle = function () {
// do stuff
};
// attach to document:ready
$(circle);
// attach to turbolinks:load
$(document).on('turbolinks:load', circle);
})(); // end of wrapper
/////////////////////////////////////////////////////////
// more isolated version, pass dependencies as arguments so
// compressors can optimize better the name of the variables
(function (document, $) {
// function to execute on load
var circle = function () {
// do stuff
};
// attach to document:ready
$(circle);
// attach to turbolinks:load
$(document).on('turbolinks:load', circle);
})(document, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment