Created
May 8, 2013 02:58
-
-
Save giosakti/5537888 to your computer and use it in GitHub Desktop.
auth-token-handler.js.coffee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory "AuthTokenHandler", () -> | |
authTokenHandler = {} | |
authToken = "none" | |
# Getter / setter | |
authTokenHandler.get = () -> | |
return authToken | |
authTokenHandler.set = (newAuthToken) -> | |
authToken = newAuthToken | |
# Wrap every actions with tokenWrapper function | |
authTokenHandler.wrapActions = (resource, actions) -> | |
wrappedResource = resource | |
i=0 | |
while i < actions.length | |
tokenWrapper wrappedResource, actions[i] | |
i++ | |
wrappedResource | |
# tokenWrapper function | |
tokenWrapper = (resource, action) -> | |
resource['_' + action] = resource[action] | |
if action in ['save', 'update'] | |
resource[action] = (params, data, success, error) -> | |
resource['_' + action] angular.extend({}, params or {}, | |
auth_token: authTokenHandler.get() | |
), data, success, error | |
else | |
resource[action] = (params, success, error) -> | |
resource["_" + action] angular.extend({}, params or {}, | |
auth_token: authTokenHandler.get() | |
), success, error | |
return authTokenHandler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment