Skip to content

Instantly share code, notes, and snippets.

@EmmanuelBeziat
Last active April 16, 2021 12:58
Show Gist options
  • Save EmmanuelBeziat/e6c5bef5ae72bcec8e820d6f473835bf to your computer and use it in GitHub Desktop.
Save EmmanuelBeziat/e6c5bef5ae72bcec8e820d6f473835bf to your computer and use it in GitHub Desktop.
/*!
*
* Version :
* Emmanuel B. (www.emmanuelbeziat.com)
* https://github.com/EmmanuelBeziat/
**/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.MyPlugin = factory();
}
}(this, function () {
var MyPlugin = function (el, options){
'use strict';
var self = Object.create(MyPlugin.prototype);
/**
* Default settings
*/
self.options = {
myOption: '',
callbackFunction: null
};
/**
* User defined options
*/
if (options) {
Object.keys(options).forEach(function (key){
self.options[key] = options[key];
});
}
/**
* By default, search for an item with '' class
*/
if (!el) {
self.form = document.querySelector('.class');
}
if (el && 'string' === typeof el) {
self.form = document.querySelector(el);
}
else if (el && 'object' === typeof el) {
self.form = el;
}
else {
throw new Error('[MyPlugin] Unable to get a valid object');
}
var init = function () {
build.call(this);
};
/**
*
* Allow callback
*/
function sampleFunction() {
// callback
if ('function' === typeof self.options.callbackFunction) {
self.options.callbackFunction();
}
}
/**
* Main build function
* 1.
* 2.
*/
function build() {
}
init();
return self;
};
return MyPlugin;
}));
@Igcorreia
Copy link

Hi, do you suggest we change this line:

/**
* By default, search for an item with '' class
*/
if (!el) {
self.form = document.querySelector('.myplugin');
}

because if I do:

MyPlugin()

I get and error:
[MyPlugin] Unable to get a valid object

https://codepen.io/igcorreia/pen/84184900388b19742244e142d4803556?editors=1011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment