Skip to content

Instantly share code, notes, and snippets.

@corkupine
Created August 24, 2012 22:04
Show Gist options
  • Save corkupine/3456249 to your computer and use it in GitHub Desktop.
Save corkupine/3456249 to your computer and use it in GitHub Desktop.
Fixed caching UserDataService
mod = angular.module('myApp.services', ['ngResource']);
mod.factory('UserData', ($resource,$location) ->
return $resource('http://' + $location.host() + '/Services/userinfo')
)
.factory('UserDataStore', ($resource, $location, UserData, $q, $rootScope) ->
GetUserDataFromXhr = () ->
deferred = $q.defer()
UserData.get((userdata) ->
deferred.resolve userdata.userInfo
)
return deferred.promise
GetUserDataFromStore = () ->
deferred = $q.defer()
Lawnchair ()->
@get 'userdata',(result) ->
deferred.resolve result
return deferred.promise
ReturnStoreResultOrXHRPromise = (result) ->
if not result
xPromise = GetUserDataFromXhr()
xPromise.then(SaveDataToLawnchair)
return xPromise
else
resultObj = JSON.parse(result.value)
return resultObj
SaveDataToLawnchair = (result) ->
resultString = JSON.stringify(result)
userdata = {key:"userdata", value:resultString}
Lawnchair () ->
@save userdata
return {
Clear: () ->
Lawnchair ()->
@remove('userdata')
Data: () ->
sPromise = GetUserDataFromStore()
combinedPromise = sPromise.then(ReturnStoreResultOrXHRPromise)
return combinedPromise
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment