2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ You could check the jQuery Boilerplate for a better understanding. \$\endgroup\$ – Joseph May 13 '13 at 20:56
  • \$\begingroup\$ Thanks @JosephtheDreamer that's where I got bits for the code above. \$\endgroup\$ – mikedidthis May 14 '13 at 8:53
2
\$\begingroup\$

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 simply return the result of $.each

I like your options handling, graceful fallback to jQuery, style, it's all good.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.