User loading now have async callbacks

This commit is contained in:
Ruben Vereecken 2014-07-07 12:03:48 +02:00
parent ec78f95da1
commit 75b738f126
2 changed files with 8 additions and 5 deletions

View file

@ -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

View file

@ -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