-
-
Save cliffmeyers/b25653d7fc54d00184ce to your computer and use it in GitHub Desktop.
Switch between mock and real service implementation
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
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