Skip to content

Instantly share code, notes, and snippets.

@geronimod
Created January 25, 2013 15:30
Show Gist options
  • Save geronimod/4635263 to your computer and use it in GitHub Desktop.
Save geronimod/4635263 to your computer and use it in GitHub Desktop.
ICB resources
var ICB = {
apiUri: '/hcd',
cache: {},
parents: {
industry : undefined,
sup_sector : "industry",
sector : "sup_sector",
sub_sector : "sector"
},
resourceApiId: {
industry : "industries",
sup_sector : "sup_sectors",
sector : "sectors",
sub_sector : "sub_sectors",
company : "companies",
product : "products"
},
// TODO: change in rest api for product
resourceDataId: {
industries : "industries",
sup_sectors : "sup_sectors",
sectors : "sectors",
sub_sectors : "sub_sectors",
companies : "companies",
products : "product"
},
setCache: function(key, value) {
console.log("cached ", key, value);
this.cache[key] = value;
},
getCache: function(key) {
if (this.cache[key]) {
console.log("cache hit ", key);
return this.cache[key];
}
},
resources: function(type, id, resource, options) {
options = options ? _.clone(options) : {};
options.ancestors = options.ancestors || false;
var success = options.success,
error = options.error;
var self = this, fullpath = [],
url = [this.apiUri, this.resourceApiId[type], id].join("/"),
cacheKey = [type, id].join("-"),
resourceDataId = this.resourceDataId[resource];
if (this.getCache(cacheKey)) {
success(this.cache[cacheKey].results[resourceDataId]);
} else {
Service.call(url, {
success: function(data) {
self.setCache(cacheKey, data);
console.log("ICB#resources:success", url, data);
if (options.ancestors) {
var arr = data.results[resourceDataId];
// sync the calls for all the resources to prevent return early without the ancestors
function sync(i, callback) {
var e = arr[i];
if (!e) return callback(arr);
var type = _.invert(self.resourceApiId)[resource];
self.ancestors(e.uid, type, [], function(ancestors) {
debugger
e.ancestors = ancestors;
sync(--i, callback);
});
// TODO: this is temp, we need to sync the ancestor calls
// window.setTimeout(function(){
// }, 15000)
}
// call for the first value in the array, the next calls son sync inside the sync func
sync(arr.length-1, success);
} else {
var categories = [{"type": "category", "name": "Data storage device", "uid": "data_storage_device"}, {"type": "category", "name": "Soft drink", "uid": "soft_drink"}, {"type": "category", "name": "Headphones", "uid": "headphones"}, {"type": "category", "name": "Electronic musical instrument", "uid": "electronic_musical_instrument"}, {"type": "category", "name": "Rolling Tobacco", "uid": "762671"}];
success(resource === 'categories' ? categories : data.results[resourceDataId]);
}
},
error: error
});
}
},
ancestors: function(id, type, currentAncestors, callback) {
var self = this,
url = [this.apiUri, this.resourceApiId[type], id].join("/"),
cacheKey = [type, id].join("-"),
parentDataId = self.resourceApiId[self.parents[type]];
if (this.getCache(cacheKey)) {
var ancestor = _.first(this.cache[cacheKey].results[parentDataId]);
if (ancestor) {
currentAncestors.push(ancestor);
self.ancestors(ancestor.uid, ancestor.type, currentAncestors, callback);
} else {
// if we are on top of the tree we finish so we callback
callback(currentAncestors);
}
} else {
Service.call(url, {
success: function(data) {
self.setCache(cacheKey, data);
// NOTE: there are repeated values for the ancestor
var ancestor = _.first(data.results[parentDataId]);
if (ancestor) {
currentAncestors.push(ancestor);
// recursive call to get the ancestor of this resource
self.ancestors(ancestor.uid, ancestor.type, currentAncestors, callback);
} else {
// if we are on top of the tree we finish so we callback
callback(currentAncestors);
}
}
});
}
},
persist: function() {
// if (window.localStorage)
// window.localStorage.addItem('ICB', this.build());
}
}
// ICB.persist();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment