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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 13, 2013 at 19:07
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You could check the jQuery Boilerplate for a better understanding. \$\endgroup\$ Commented May 13, 2013 at 20:56
  • \$\begingroup\$ Thanks @JosephtheDreamer that's where I got bits for the code above. \$\endgroup\$ Commented May 14, 2013 at 8:53

1 Answer 1

3
\$\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.

answered Apr 11, 2014 at 20:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.