Skip to content

Instantly share code, notes, and snippets.

@ansmith
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save ansmith/8957778 to your computer and use it in GitHub Desktop.

Select an option

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.
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();
});
@grgur

grgur commented Feb 20, 2014

Copy link
Copy Markdown

Combine var maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment