Skip to content

Instantly share code, notes, and snippets.

@mistercoffee66
Last active November 6, 2019 16:03
Show Gist options
  • Select an option

  • Save mistercoffee66/3d9d67898a7f357f1a5833b7b8199ff8 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Thanks for this helpful gist!

@hemantsan

Copy link
Copy Markdown

Not working for me.

@omnajjar

omnajjar commented Dec 28, 2018

Copy link
Copy Markdown

Thanks useful tip!!

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