Created
May 3, 2016 21:07
-
-
Save TiagoWinehouse/cf7d714e21ec755c9981772b9634ff67 to your computer and use it in GitHub Desktop.
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
angular.module('app').service('commonService', commonService); | |
function commonService() { | |
//DFS for fixing JSON references | |
var elements = {} | |
this.fixReferences = function (json) { | |
var tree = json; | |
for (var x in tree) { | |
if ((typeof (tree[x]) === 'object') && (tree[x] !== null)) { | |
var result = dfsVisit(tree[x]); | |
tree[x] = result; | |
} | |
} | |
return tree; | |
} | |
function dfsVisit(tree) { | |
for (var x in tree) { | |
if ((typeof (tree[x]) === 'object') && (tree[x] !== null)) { | |
var result = dfsVisit(tree[x]); | |
tree[x] = result; | |
} | |
} | |
if (tree["$ref"] !== undefined) { | |
var ref = tree.$ref; | |
if (elements[ref] !== undefined) { | |
tree = elements[ref]; | |
} | |
} else if (tree["$id"] !== undefined) { | |
var element = tree; | |
elements[element.$id] = element; | |
} | |
return tree; | |
} | |
} | |
angular.module('app').factory('yourService', yourService); | |
/*@ngInject*/ | |
function yourService($http, commonService) { | |
var service = { | |
get: get | |
}; | |
return service; | |
function get() { | |
return $http.get('Your url').then(function (response) { | |
var fixedData = commonService.fixReferences(response.data); | |
return fixedData; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment