I am currently creating a JQuery Plugin that support data-attributes for defining options. I scoured the internet for sources on how to propery do it, but cannot find any.
What I have done so far:
HTML:
<table id="myTable" data-columns='[{ "header": "Header1" },
{ "header": "Header2" },
{ "header": "Header3" }]'>
</table>
Plugin:
$.fn.myPlugin = function () {
columns:""
}
var self = $(this)
var foo = self.data("columns")
//..some code to create columns
Plugin Usage:
$("#myTable").myPlugin()
self.data()call will return an array of objects.$(this), becausethiswill point to the jQuery object and not an individual element. In fact, the standard practice for a plugin that could operate meaningfully on more than one matched element in the jQuery object should use a.each()loop, and also of course return the jQuery object as its result so that it can be used in a call chain.