-
-
Save jacquescrocker/912762 to your computer and use it in GitHub Desktop.
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
$ = jQuery | |
$.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 |
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
(function() { | |
var $; | |
$ = jQuery; | |
$.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); |
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
$ = jQuery | |
$ -> | |
$('#world').myplugin(); |
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
(function() { | |
var $; | |
$ = jQuery; | |
$(function() { | |
return $('#world').myplugin(); | |
}); | |
}).call(this); |
True, I did see a few things about that, but at the same time, my example worked in a little test html file of mine without it, so I decided to leave it out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really should be setting $ explicitly on top. See: https://gist.github.com/912762
This is similar to the pattern you see in jQuery often: