Skip to content

Commit

Permalink
Correctly passing options from html tag to custom plugins using jquer…
Browse files Browse the repository at this point in the history
…y.data()
  • Loading branch information
Joey Trapp committed Apr 12, 2011
1 parent fe15132 commit 22efd0c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion index.html
Expand Up @@ -4,10 +4,12 @@
<title>jQuery Selector Test</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.pluginLoader.js" type="text/javascript"></script>
<script src="params.js" type="text/javascript"></script>
<script src="content.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
</head>
<body>
<div class="required" data-content data-name="joey"></div>
<div class="required" data-content='{"content":"This is content from the html tag"}' data-name="joey"></div>
<div id="container" data-params='{"content":"This is the content that will fill the tag"}'></div>
</body>
</html>
6 changes: 2 additions & 4 deletions jquery.pluginLoader.js
Expand Up @@ -23,14 +23,12 @@
var loader = {};
var methods = {
init: function($this, plugins) {
console.log(plugins);
return $this.each(function(index) {
var self = $(this);
var attrs = self[0].attributes;
for (var i = 0; i < attrs.length; i++) {
var attrName = attrs[i].nodeName;
if (!attrName.match(/data-(.)/) || plugins.indexOf(attrName.replace(/data-/, '')) === -1) {
console.log(attrName + ' didn\'t match');
continue;
}
var parsed = attrs[i].nodeName.replace(/data-/, '');
Expand All @@ -40,7 +38,6 @@ var methods = {
if (self[parsed]) {
self[parsed](self.data(parsed));
}
console.log('no loader method defined for ' + attrs[i].nodeName.replace(/data-/, ''));
}
}
});
Expand All @@ -54,6 +51,7 @@ $.fn.pluginLoader = function(plugins, options) {
}
loader = $.extend(loader, options);
var find = plugins.map(function(el) { return '[data-'+el+']'; }).join(', ');
return methods.init($(find), plugins);
methods.init($(find), plugins);
return this;
}
})(jQuery);
6 changes: 5 additions & 1 deletion scripts.js
@@ -1,5 +1,9 @@
(function($) {
$(document).ready(function() {
$(document).pluginLoader(['content ', ' params'], {'content': function(el, opt) { console.log('content loader method called'); }});
$(document).pluginLoader('content, params',{
'content': function(el, opt) {
el.content(opt);
}
});
});
})(jQuery);

0 comments on commit 22efd0c

Please sign in to comment.