Created
January 29, 2013 07:50
-
-
Save tauren/4662555 to your computer and use it in GitHub Desktop.
AMD module that extracts data attributes from script tag that contains data-main attribute and returns an object containing all of the data values.
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
/* | |
* @description Extracts data attributes from script tag that contains data-main attribute | |
* and returns an object containing all of the data values. | |
* @author Tauren Mills (github.com/tauren) | |
*/ | |
define(function() { | |
// Get all of the script tags | |
var scripts = document.getElementsByTagName('script'); | |
var data = {}; | |
// Iterate through the script tags in reverse | |
for (var i = scripts.length-1; i>0; i--) { | |
// Get all attributes of the tag | |
var attrs = scripts[i].attributes; | |
// Check if the data-main attribute exists | |
if (attrs && attrs['data-main']) { | |
// Iterate all attributes | |
for (var attr, i=0, l=attrs.length; i<l; i++) { | |
// Get an attribute | |
attr = attrs.item(i); | |
// Check if it is a data- attribute | |
if(/^data-/.test(attr.name)) { | |
// Remove 'data-' from the name and add the | |
// name/value pair to the results | |
data[attr.name.split('-')[1]] = attr.value; | |
} | |
} | |
// Break out early, retuning the found data | |
return data; | |
} | |
} | |
// No data was found, return empty object | |
return data; | |
}); |
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
<script | |
data-main="/fancy/js/main" | |
data-app="FancyApp" | |
data-token="abcdef" | |
data-env="dev" | |
src="/libs/requirejs/require.js"> | |
</script> |
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
// Main module, located in fancy/js/main.js | |
requirejs.config({ | |
baseUrl: '/', | |
paths: { | |
'data': 'common/js/data', | |
} | |
}); | |
require(['data','fancy/js/other'], function(data, other) { | |
console.log('data attributes',data); | |
other.run(); | |
}); |
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
// Other module, located in fancy/js/other.js | |
define(['data'], function(data) { | |
return { | |
run: function() { | |
console.log('Running '+data.app+' in '+ | |
data.env+' mode with token '+data.token); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment