Skip to content

Instantly share code, notes, and snippets.

@sebastien-p
Created September 11, 2011 21:09
Show Gist options
  • Save sebastien-p/1210134 to your computer and use it in GitHub Desktop.
Save sebastien-p/1210134 to your computer and use it in GitHub Desktop.
jQuery CORD plugin boilerplate
/*!
* Copyright (c) 2011 Sebastien P.
* http://twitter.com/_sebastienp
* Licensed under the MIT license
*
* Wed Sep 14 2011 11:53:40 GMT+0200 (CEST)
*/
;!function (PLUGIN_NAME, WINDOW, DOCUMENT, TRUE, FALSE, NULL, UNDEFINED, $, PLUGIN_DEFAULTS, j) {
if($=WINDOW.Zepto||WINDOW.jQuery)(j=$.extend)($.fn[PLUGIN_NAME]=function(b,c,d,methods,f,g,i,k){
/*** Private methods ***/
function myPrivateMethod() {
}
/***********************/
b=(methods={
/*** Public methods ***/
create: function (data) {
},
option: function (data) {
},
refresh: function (data) {
},
destroy:function (data) {
}
/**********************/
})[b]||(c=/ct]$/.test({}.toString.call(c=c||b))&&c,methods.create);for(f=0;(g=this.eq(f++))[0];)if(!(i=g.data(PLUGIN_NAME))!=(b!=methods.create||!i&&!g.data(PLUGIN_NAME,i={settings:j({},$.fn[PLUGIN_NAME].defaults,c)}))){if(b==methods.option){k=i.settings;if(!(1 in arguments))return k;if(typeof c=="string")if(2 in arguments)k[c]=d;else return k[c];else j(k,c)}else b==methods.destroy&&g.data(PLUGIN_NAME,"");b.call(g,i)}return this},{
/*** Public properties ***/
defaults: j({},PLUGIN_DEFAULTS={
}),
version: "1.0.0"
/*************************/
})}("myPlugin", window, document, !0, !1, null);
/*!
* Copyright (c) 2011 Sebastien P.
* http://twitter.com/_sebastienp
* Licensed under the MIT license
*
* Tue Sep 13 2011 13:55:25 GMT+0200 (CEST)
*/
;!function (PLUGIN_NAME, WINDOW, DOCUMENT, TRUE, FALSE, NULL, UNDEFINED, $, PLUGIN_DEFAULTS, extend) {
// 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 = $.extend)(
$.fn[PLUGIN_NAME] = function (method, options, value, methods, _i, _that, _data, _settings) {
/*** Private methods ***/
function privateMethod () {
// ...
}
/*** Public methods ***/
method = (methods = {
// $(element).myPlugin(["create",][options]) -> chainable jQuery object
create: function (data) {
// '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 (data) {
// 'this' refers to $(element) ...
},
// $(element).myPlugin("refresh") -> chainable jQuery object
refresh: function (data) {
// 'this' refers to $(element) ...
},
// $(element).myPlugin("destroy") -> chainable jQuery object
destroy: function (data) {
// 'this' refers to $(element) ...
}
})[method] || (
options = /ct]$/.test({}.toString.call(options = options || method)) && options,
methods.create
);
/*** Methods logic ***/
for (_i = 0; (_that = this.eq(_i++))[0];)
if (!(_data =
// Maybe kind of a workaround for the Zepto '$.fn.data' bug ? See above.
//JSON.parse(
_that.data(PLUGIN_NAME)
//)
) != (method != methods.create || !(_data || _that.data(PLUGIN_NAME,
// Maybe kind of a workaround for the Zepto '$.fn.data' bug ? See above.
//JSON.stringify(
// Uncomment below if needed but deep copy isn't currently a part of Zepto's API
_data = { settings: extend(/*TRUE, */{}, $.fn[PLUGIN_NAME].defaults, options) }
//)
)))) {
if (method == methods.option) {
_settings = _data.settings;
if (!(1 in arguments)) return _settings;
if (typeof options == "string") {
if (!(2 in arguments)) return _settings[options];
_settings[options] = value
} else extend(_settings, options)
// '$.fn.removeData' would have been better but isn't currently a part of Zepto's API
} else method == methods.destroy && _that.data(PLUGIN_NAME, "");
method.call(_that, _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