-
-
Save pgroot91/8c974b153e7291143c49eb1ba5e64029 to your computer and use it in GitHub Desktop.
Add data to HTML according to object's property name
This file contains 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
// Add data to HTML according to object's property name | |
function addContentFromDataToHtml(object, dataSet) { | |
recursiveObject({object: object, callback: addContentFromDataToHtmlCallback, dataSet: dataSet}); | |
} | |
var recursiveObject = function(recursiveParams) { | |
if (recursiveParams.parentsItem) { | |
recursiveParams.parentsItem = recursiveParams.parentsItem+'.'; | |
} else { | |
recursiveParams.parentsItem = ''; | |
} | |
if (typeof(recursiveParams.object) == 'object') { | |
for(var item in recursiveParams.object){ | |
recursiveObject({object: recursiveParams.object[item], parentsItem: recursiveParams.parentsItem+item, callback: recursiveParams.callback}); | |
recursiveParams.callback(recursiveParams.parentsItem+item, recursiveParams.object[item], recursiveParams.dataSet); | |
} | |
} | |
} | |
function addContentFromDataToHtmlCallback(key, value, dataSet) { | |
$((dataSet?'[data-set="'+dataSet+'"]':'')+' [data-content="'+key+'"]').text(value); | |
} |
This file contains 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
<div data-set="group-name"> | |
<div data-content="propertyOne"></div> | |
<div data-content="propertyTwo.sub.sub.sub"></div> | |
</div> |
This file contains 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
getSomething(callbackGetSomething); | |
function callbackGetSomething(dataObject) { | |
addContentFromDataToHtml(dataObject 'group-name'); | |
} |
This file contains 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
function getSomething(callback) { | |
var apiEndpoint = ''; | |
$.get(apiEndpoint, null, function(apiResult) { | |
callback(apiResult); | |
}).fail(function(error) { | |
return error; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment