Last active
August 29, 2015 13:56
-
-
Save ansmith/8957778 to your computer and use it in GitHub Desktop.
This is a sample Angular service, designed to expose only as much as desired and to be able to easily call "public" and "private" functions easily from within the service.
This file contains 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
myApp.factory('MyService', function($http, $q) { | |
//Local vars | |
var state = {}; | |
//Publicly exposed functions | |
var me = function() {}; | |
me.prototype = { | |
getState: function(key) { | |
return state[key]; | |
}, | |
save: function(key) { | |
var deferred = $q.defer(), | |
options = this.getState(key); | |
doRequest(options).then( | |
function(data, status, headers) { | |
//Do work | |
d.resolve(data); | |
}, | |
function(data, status, headers) { | |
d.reject(data); | |
} | |
); | |
return deferred.promise; | |
} | |
}; | |
//Private helper functions | |
function doRequest(options) { | |
return $http(options); | |
} | |
return new me(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Combine
var
maybe?