codecombat/app/core/NameLoader.coffee

27 lines
684 B
CoffeeScript
Raw Normal View History

CocoClass = require 'core/CocoClass'
2014-04-11 17:19:17 -04:00
namesCache = {}
class NameLoader extends CocoClass
loadNames: (ids) ->
toLoad = _.uniq (id for id in ids when not namesCache[id])
2014-04-11 17:19:17 -04:00
return false unless toLoad.length
jqxhrOptions = {
2014-06-30 22:16:26 -04:00
url: '/db/user/x/names',
type: 'POST',
data: {ids: toLoad},
success: @loadedNames
}
return jqxhrOptions
2014-04-11 17:19:17 -04:00
loadedNames: (newNames) =>
_.extend namesCache, newNames
2014-06-30 22:16:26 -04:00
2016-08-17 17:15:36 -04:00
getName: (id) ->
if namesCache[id]?.firstName and namesCache[id]?.lastName
return "#{namesCache[id]?.firstName} #{namesCache[id]?.lastName}"
namesCache[id]?.firstName or namesCache[id]?.name or id
2014-04-11 17:19:17 -04:00
module.exports = new NameLoader()