Skip to content

Instantly share code, notes, and snippets.

@cliffmeyers
Last active December 22, 2015 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffmeyers/b25653d7fc54d00184ce to your computer and use it in GitHub Desktop.
Save cliffmeyers/b25653d7fc54d00184ce to your computer and use it in GitHub Desktop.
Switch between mock and real service implementation
var donutModule = angular.module('donuts', []);
window.DonutRepository = function($http) {
this.$http = $http;
this.fetchDonuts = function(size, filling, glaze, hasHole) {
var url = '/api/donuts/list?size=' + size +
'&filling=' + filling +
'&glaze=' + glaze +
'&hole=' + hasHole ? 1 : 0;
// invoke real service
return this.$http.get(url)
.then(function(response) {
return response.data.results;
});
};
}
window.DonutRepositoryMock = function($http) {
this.$http = $http;
this.fetchDonuts = function(size, filling, glaze, hasHole) {
// just get data from a flat JSON file
return this.$http.get('/mockdata/donut/fetch.json')
.then(function(response) {
return response.data.results;
});
};
}
donutModule.factory('donutRepository',
['$http', 'configModel', function($http, configModel) {
if (configModel.mocksEnabled) {
return new DonutRepositoryMock($http);
} else {
return new DonutRepository($http);
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment