Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example jQuery plugin written in CoffeeScript, and the resulting compiled JavaScript file.
$.fn.extend
myplugin: (options) ->
self = $.fn.myplugin
opts = $.extend {}, self.default_options, options
$(this).each (i, el) ->
self.init el, opts
self.log el if opts.log
$.extend $.fn.myplugin,
default_options:
color: 'red'
log: true
init: (el, opts) ->
this.color el, opts
color: (el, opts) ->
$(el).css('color', opts.color)
log: (msg) ->
console.log msg
(function() {
$.fn.extend({
myplugin: function(options) {
var opts, self;
self = $.fn.myplugin;
opts = $.extend({}, self.default_options, options);
return $(this).each(function(i, el) {
self.init(el, opts);
if (opts.log) {
return self.log(el);
}
});
}
});
$.extend($.fn.myplugin, {
default_options: {
color: 'red',
log: true
},
init: function(el, opts) {
return this.color(el, opts);
},
color: function(el, opts) {
return $(el).css('color', opts.color);
},
log: function(msg) {
return console.log(msg);
}
});
}).call(this);
$ ->
$('#world').myplugin();
(function() {
$(function() {
return $('#world').myplugin();
});
}).call(this);
@shouichi

This comment has been minimized.

shouichi commented Jul 10, 2011

Nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment