From 202d4fdcefbeba6bcc38f511123d9b4ec05fc3de Mon Sep 17 00:00:00 2001 From: Lim Han Date: Sun, 8 Feb 2015 18:15:54 +0800 Subject: [PATCH] LH :: Fix bug with mock-data loading due to timing --- src/main/webapp/js/app.js | 2 +- src/main/webapp/services/services.js | 44 ++++++++++++++++------------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/main/webapp/js/app.js b/src/main/webapp/js/app.js index e825a99c2..95e009a3a 100644 --- a/src/main/webapp/js/app.js +++ b/src/main/webapp/js/app.js @@ -2,7 +2,7 @@ var app = angular.module('spring-petclinic', ['ui.router','ui.router.stateHelper /** Start of Configurable constants **/ -app.constant('useMockData', true); +app.constant('useMockData', false); app.constant('context', '/petclinic'); /** End of Configurable constants **/ diff --git a/src/main/webapp/services/services.js b/src/main/webapp/services/services.js index 5368f3af5..776f63fea 100644 --- a/src/main/webapp/services/services.js +++ b/src/main/webapp/services/services.js @@ -18,28 +18,36 @@ var Visit = ['$resource','context', function($resource, context) { return $resource(context + '/api/pets/:petId/visits', {petId : '@id'}); }]; -var MockService = ['$httpBackend', '$http', 'context', function($httpBackend, $http, context) { +var MockService = ['$httpBackend', '$http', '$q', 'context', function($httpBackend, $http, $q, context) { return { mock : function(useMockData) { - if(useMockData) { - $http.get(context + '/static/mock-data/pets.json').success(function(data) { - console.log("Mocking /api/pets"); - $httpBackend.whenGET(context + '/api/pets').respond(data); - }); - $http.get(context + '/static/mock-data/vets.json').success(function(data) { - console.log("Mocking /api/vets"); - $httpBackend.whenGET(context + '/api/vets').respond(data); - }); - $http.get(context + '/static/mock-data/owners.json').success(function(data) { - console.log("Mocking /api/owners"); - $httpBackend.whenGET(context + '/api/owners').respond(data); - }); - } + var passThroughRegex = new RegExp('/static/mock-data/|components/'); + $httpBackend.whenGET(passThroughRegex).passThrough(); - console.log("Setting up passthrough for other urls"); - var passThroughRegex = new RegExp('/'); - $httpBackend.whenGET(passThroughRegex).passThrough(); + if(useMockData) { + $q.defer(); + $q.all([ + $http.get(context + '/static/mock-data/pets.json'), + $http.get(context + '/static/mock-data/vets.json'), + $http.get(context + '/static/mock-data/owners.json') + ]).then(function(data) { + console.log("Mocking /api/pets"); + $httpBackend.whenGET(context + '/api/pets').respond(data[0].data); + console.log("Mocking /api/vets"); + $httpBackend.whenGET(context + '/api/vets').respond(data[1].data); + console.log("Mocking /api/owners"); + $httpBackend.whenGET(context + '/api/owners').respond(data[2].data); + + console.log("Setting up passthrough for other urls"); + var passThroughRegex = new RegExp('/'); + $httpBackend.whenGET(passThroughRegex).passThrough(); + }); + } else { + console.log("Setting up passthrough for other urls"); + var passThroughRegex = new RegExp('/'); + $httpBackend.whenGET(passThroughRegex).passThrough(); + } } } }]; \ No newline at end of file