Skip to content

Instantly share code, notes, and snippets.

@noherczeg
Last active February 28, 2017 10:05
Show Gist options
  • Select an option

  • Save noherczeg/f0a7632ba4b5d0b7c4982539434aa386 to your computer and use it in GitHub Desktop.

Select an option

Save noherczeg/f0a7632ba4b5d0b7c4982539434aa386 to your computer and use it in GitHub Desktop.
/**
* @param {*} class Class of instance
* @param {*} root Data holder instance
* @param {*} relationName name of Meta relation, gets mapped to e.g. Single, List
*/
SomeService.prototype.fetchRecursive = function (clazz, root, relationName) {
let vm = this;
let deferred = this.$q.defer();
let service = this.$injector.get(clazz.serviceInstanceId());
let relation = clazz.getAttributeByName(relationName);
service
.getListItems(relation, root.id)
.then(function(response){
vm.$q
.all(response.data.map((child) => {
if (!root.hasOwnProperty(relation.name)) {
root[relation.name] = [];
}
root[relation.name].push(child);
child.parent = root;
let childClass = vm.metaContainer.getFromUniquesById(child.xmiid);
return vm.fetchRecursive(childClass, child, relationName);
}))
.then(() => {
deferred.resolve(root);
}, (err) => {
deferred.reject(err);
});
});
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment