Cache item type names to prevent repeated DB lookups in ItemTypes.getTypeName()

This commit is contained in:
Dan Stillman 2006-06-12 12:59:25 +00:00
parent b1389bfc61
commit 5a045f5b50

View file

@ -1624,6 +1624,10 @@ Scholar.Creators = new function(){
Scholar.ItemTypes = new function(){
var _itemTypes = new Array();
var _itemTypesLoaded;
var self = this;
this.getTypes = getTypes;
this.getTypeName = getTypeName;
@ -1633,8 +1637,25 @@ Scholar.ItemTypes = new function(){
}
function getTypeName(itemTypeID){
return Scholar.DB.valueQuery('SELECT typeName FROM itemTypes '
+ 'WHERE itemTypeID=' + itemTypeID);
if (!_itemTypesLoaded){
_load();
}
if (!_itemTypes[itemTypeID]){
Scholar.debug('Invalid item type ' + itemTypeID, 1);
}
return _itemTypes[itemTypeID];
}
function _load(){
var types = self.getTypes();
for (i in types){
_itemTypes[types[i]['id']] = types[i]['name'];
}
_itemTypesLoaded = true;
}
}