Created
September 11, 2011 21:09
-
-
Save sebastien-p/1210134 to your computer and use it in GitHub Desktop.
jQuery CORD plugin boilerplate
This file contains hidden or 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
/*! | |
* Copyright (c) 2011 Sebastien P. | |
* http://twitter.com/_sebastienp | |
* Licensed under the MIT license | |
*/ | |
;!function (PLUGIN_NAME, WINDOW, DOCUMENT, TRUE, FALSE, NULL, UNDEFINED, $, PLUGIN_DEFAULTS) { | |
// Zepto is not currently fully supported because of a weird implementation of '$.fn.data' | |
// which causes '$(element).data("namespace", {}).data("namespace")' to return '[object Object]' | |
if ($ = WINDOW.Zepto || WINDOW.jQuery) | |
$.extend( | |
$.fn[PLUGIN_NAME] = function (method, options, value, _methods, _i, _that, _data) { | |
/*** Private methods ***/ | |
function privateMethod () { | |
// ... | |
} | |
/*** Public methods ***/ | |
method = (_methods = { | |
// $(element).myPlugin(["create",][options]) -> chainable jQuery object | |
create: function (settings) { | |
// 'this' refers to $(element) ... | |
}, | |
// $(element).myPlugin("option") -> object | |
// $(element).myPlugin("option", options) -> chainable jQuery object | |
// $(element).myPlugin("option", "optionName") -> value | |
// $(element).myPlugin("option", "optionName", value) - > chainable jQuery object | |
option: function (settings) { | |
// 'this' refers to $(element) ... | |
}, | |
// $(element).myPlugin("refresh") -> chainable jQuery object | |
refresh: function (settings) { | |
// 'this' refers to $(element) ... | |
}, | |
// $(element).myPlugin("destroy") -> chainable jQuery object | |
destroy: function (settings) { | |
// 'this' refers to $(element) ... | |
} | |
})[method] || (options = options || method, _methods.create); | |
/*** Methods logic ***/ | |
for (_i = 0; (_that = this.eq(_i++))[0];) | |
if (!(_data = _that.data(PLUGIN_NAME)) == (method == _methods.create)) { | |
if (method == _methods.option) { | |
if (!(1 in arguments)) return _data; | |
if (typeof options == "string") { | |
if (!(2 in arguments)) return _data[options]; | |
_data[options] = value | |
} | |
} | |
method.call(_that, method != _methods.destroy ? $.extend( | |
//TRUE, // Uncomment if needed but deep copy is not currently a part of Zepto's API | |
_data || _that.data(PLUGIN_NAME, _data = {}) && _data, $.fn[PLUGIN_NAME].defaults, | |
($.isPlainObject || $.isObject || function (object) { | |
return /ct]$/.test({}.toString.call(object)) | |
})(options) && options | |
) : _that.data(PLUGIN_NAME, "") && _data) | |
} | |
return this | |
}, { | |
/*** Public properties ***/ | |
defaults: $.extend({}, PLUGIN_DEFAULTS = { | |
// key: value ... | |
}), | |
version: "1.0.0" | |
} | |
) | |
}("myPlugin", window, document, !0, !1, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment