Last active
November 6, 2019 16:03
-
-
Save mistercoffee66/3d9d67898a7f357f1a5833b7b8199ff8 to your computer and use it in GitHub Desktop.
ES6 module importing jQuery plugin
This file contains hidden or 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
//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) | |
} |
This file contains hidden or 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
//jquery-dosomething.js | |
//before modification | |
$.fn.dosomething = function() { | |
+function($) { | |
var $this = $(this); | |
var newText = $this.data('text'); | |
console.log(newText); | |
return this; | |
}(jQuery) | |
}; |
This file contains hidden or 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
//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" |
Not working for me.
Thanks useful tip!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this helpful gist!