Last active
August 29, 2015 13:55
-
-
Save morganney/8757562 to your computer and use it in GitHub Desktop.
Function to Insert Gists Dynamically.
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
| /* | |
| * Embeds Gists into specified DOM elements after fetching them. | |
| * Assumes Gists are to be embedded into elements with an HTML5 data-* attribute, | |
| * with a value equal to a public Gist id. | |
| * | |
| * @param {String:dataAttrName} The data attribute name value. | |
| * For example <code data-gistId='12345'></code> means the dataAttrName === "gistId". | |
| */ | |
| function embedGists(dataAttrName) { | |
| var dataAttr = "data-" + dataAttrName; | |
| $("[" + dataAttr + "]").each(function(idx, el) { | |
| fetch($(el).attr(dataAttr)).then(function(gist) { | |
| addGistCss(gist.stylesheet); // Utility function to add stylesheet from GitHub. | |
| $(el).html(gist.div); | |
| }).catch(error) { | |
| $(el).html("Something went wrong: " + error.message); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment