Last active
November 22, 2016 17:17
-
-
Save romelperez/de2de9a63c3fd33c2c3ae7a53e74f402 to your computer and use it in GitHub Desktop.
A jQuery plugin boilerplate
This file contains 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
const NAME = 'pluginName'; | |
const methods = { | |
method1: function () { | |
const conf = this.data(`${NAME}-config`); | |
if (!conf) return this; | |
// | |
return this; | |
} | |
}; | |
jQuery[NAME] = { | |
CONSTANT1: '...', | |
globalMethod1: () => { | |
// | |
} | |
}; | |
jQuery.fn[NAME] = function (settings) { | |
if (!this.length) return this; | |
if (typeof settings === 'string') { | |
if (methods[settings]) { | |
const args = Array.prototype.slice.call(arguments, 1); | |
return methods[settings].apply(this, args); | |
} else { | |
throw new Error(`${NAME} method unrecognized "${settings}".`); | |
} | |
} | |
if (this.data(`${NAME}-config`)) return; | |
const conf = $.extend(true, { | |
// | |
}, settings); | |
this.data(`${NAME}-config`, conf); | |
this.each(function () { | |
// HTML | |
// | |
// EVENTS | |
// | |
// INIT | |
// | |
}); | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment