Skip to content

Instantly share code, notes, and snippets.

@Krizzzn
Created April 24, 2012 20:37
Show Gist options
  • Save Krizzzn/2483519 to your computer and use it in GitHub Desktop.
Save Krizzzn/2483519 to your computer and use it in GitHub Desktop.
jQuery Plugin Boilerplate with CoffeeScript
(($) ->
self = null
local_variable = null
private_methods =
register_events: ->
self.bind 'started', -> console.log 'it was started!'
null
methods =
init: (options) ->
@each ->
$this = $(this)
self = $this
private_methods.register_events()
if options.awesome?
console.log 'indeed, it is awesome!'
$this
start: ->
self?.trigger('started')
self
$.fn.PLUGIN_NAME = (method) ->
if methods[method]
methods[method].apply this, Array::slice.call(arguments, 1)
else if typeof method is "object" or not method
methods.init.apply this, arguments
else
$.error "Method " + method + " does not exist on jQuery.PLUGIN_NAME"
) jQuery
$(document).ready ->
$("div").PLUGIN_NAME(awesome: true)
$("div").PLUGIN_NAME('start')
/*
COMPILING ABOVE COFFEESCRIPT WILL RESULT IN THIS JAVASCRIPT CODE
*/
(function($) {
var local_variable, methods, private_methods, self;
self = null;
local_variable = null;
private_methods = {
register_events: function() {
self.bind('started', function() {
return console.log('it was started!');
});
return null;
}
};
methods = {
init: function(options) {
return this.each(function() {
var $this;
$this = $(this);
self = $this;
private_methods.register_events();
if (options.awesome != null) {
console.log('indeed, it is awesome!');
}
return $this;
});
},
start: function() {
if (self != null) {
self.trigger('started');
}
return self;
}
};
return $.fn.PLUGIN_NAME = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
return $.error("Method " + method + " does not exist on jQuery.PLUGIN_NAME");
}
};
})(jQuery);
$(document).ready(function() {
$("div").PLUGIN_NAME({
awesome: true
});
return $("div").PLUGIN_NAME('start');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment