mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-28 01:55:38 -05:00
User loading now have async callbacks
This commit is contained in:
parent
ec78f95da1
commit
75b738f126
2 changed files with 8 additions and 5 deletions
|
@ -33,9 +33,12 @@ module.exports = class User extends CocoModel
|
||||||
return "/file/#{photoURL}#{prefix}s=#{size}"
|
return "/file/#{photoURL}#{prefix}s=#{size}"
|
||||||
return "/db/user/#{@id}/avatar?s=#{size}"
|
return "/db/user/#{@id}/avatar?s=#{size}"
|
||||||
|
|
||||||
@getByID = (id, properties, force) ->
|
# Callbacks can be either 'success' or 'error'
|
||||||
|
@getByID = (id, properties, force, callbacks={}) ->
|
||||||
{me} = require 'lib/auth'
|
{me} = require 'lib/auth'
|
||||||
return me if me.id is id
|
if me.id is id
|
||||||
|
callbacks.success me if callbacks.success?
|
||||||
|
return me
|
||||||
user = cache[id] or new module.exports({_id: id})
|
user = cache[id] or new module.exports({_id: id})
|
||||||
if force or not cache[id]
|
if force or not cache[id]
|
||||||
user.loading = true
|
user.loading = true
|
||||||
|
@ -43,7 +46,8 @@ module.exports = class User extends CocoModel
|
||||||
success: ->
|
success: ->
|
||||||
user.loading = false
|
user.loading = false
|
||||||
Backbone.Mediator.publish('user:fetched')
|
Backbone.Mediator.publish('user:fetched')
|
||||||
#user.trigger 'sync' # needed?
|
callbacks.success arguments... if callbacks.success?
|
||||||
|
error: -> callbacks.error arguments... if callbacks.error?
|
||||||
)
|
)
|
||||||
cache[id] = user
|
cache[id] = user
|
||||||
user
|
user
|
||||||
|
|
|
@ -12,8 +12,7 @@ module.exports = class UserView extends RootView
|
||||||
@listenTo @, 'userLoaded', @onUserLoaded
|
@listenTo @, 'userLoaded', @onUserLoaded
|
||||||
|
|
||||||
# TODO Ruben Assume ID for now
|
# TODO Ruben Assume ID for now
|
||||||
@user = User.getByID(@nameOrID, {}, true) # Force fetching a user isn't really the clean way to go
|
@user = User.getByID @nameOrID, {}, true,
|
||||||
@user.fetch
|
|
||||||
success: =>
|
success: =>
|
||||||
@trigger 'userLoaded', @user
|
@trigger 'userLoaded', @user
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue