Skip to content

Instantly share code, notes, and snippets.

@mistercoffee66
Last active November 6, 2019 16:03
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mistercoffee66/3d9d67898a7f357f1a5833b7b8199ff8 to your computer and use it in GitHub Desktop.
Save mistercoffee66/3d9d67898a7f357f1a5833b7b8199ff8 to your computer and use it in GitHub Desktop.
ES6 module importing jQuery plugin
//jquery-dosomething-ES6.js
//after modification
//assumes jQuery is avail as global or via NPM etc
import jQuery from 'jquery'
export default function() {
+function($) {
var $this = $(this);
var newText = $this.data('text');
console.log(newText);
return this;
}(jQuery)
}
//jquery-dosomething.js
//before modification
$.fn.dosomething = function() {
+function($) {
var $this = $(this);
var newText = $this.data('text');
console.log(newText);
return this;
}(jQuery)
};
//my-module.js
import $ from 'jquery'
import dosomething from 'jquery-dosomething-ES6'
//bind plugin definition to jQuery
$.fn.dosomething = dosomething
//some html on our page
//<p id="my-selector" data-text="Four score and seven years ago">Here's some exciting content</p>
$('#my-selector').dosomething() //logs "Four score and seven years ago"
@adriaandotcom
Copy link

Thanks for this helpful gist!

@hemantsan
Copy link

Not working for me.

@omnajjar
Copy link

omnajjar commented Dec 28, 2018

Thanks useful tip!!

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