I am looking for a starting point to code plugins, primarily for Zepto.js (with fall back for jQuery). These will provide reusable functions for Tumblr theming.
However, I can't seem to find anything in depth about writing a plugin (with Zepto) as the example, apart from a small snippet on the Zepto site: http://zeptojs.com/
The code below is pieced together from reading various 'jQuery' plugin tutorials, but I am not 100% sure how solid it is.
;(function($){
$.extend($.fn, {
pluginName: function(el, options) {
// Set defaults
this.defaults = {
option: 'option',
onComplete: function() {}
};
// Combine defaults / options
var settings = $.extend({}, this.defaults, options);
// Do stuff
$.each(this, function() {
});
// Call back if needed
settings.onComplete.call(this);
// Return `this` for chain ability
return this;
}
});
}(window.Zepto || window.jQuery));
According to the Zepto documentation, my plugins method will be added to the Zepto object. Is this acceptable?
I would also like the option to use private / public functions contained in the plugin for flexibility.
I apologise if my questions come across as confusing or novice, but any help / improvements would be greatly appreciated.
-
1\$\begingroup\$ You could check the jQuery Boilerplate for a better understanding. \$\endgroup\$Joseph– Joseph2013年05月13日 20:56:05 +00:00Commented May 13, 2013 at 20:56
-
\$\begingroup\$ Thanks @JosephtheDreamer that's where I got bits for the code above. \$\endgroup\$mikedidthis– mikedidthis2013年05月14日 08:53:02 +00:00Commented May 14, 2013 at 8:53
1 Answer 1
This looks fine to me, but then again it's sample (boilerplate) code ;)
- Consider
"use strict"
; - If it was not for the
onComplete
, you could simplyreturn
the result of$.each
I like your options handling, graceful fallback to jQuery, style, it's all good.