Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from TAMULib/sprint5-50-61-resolving_todo
Implement fetching on change and dimplify internal request service design
- Loading branch information
Showing
10 changed files
with
231 additions
and
251 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,102 @@ | ||
app.service('ProductsService', function ($q, ProductRepo, WsApi) { | ||
var productsService = this; | ||
var service = this; | ||
|
||
var products = {}; | ||
var products = []; | ||
var remoteProducts = {}; | ||
|
||
var defer = $q.defer(); | ||
|
||
var process = function (response) { | ||
var apiRes = angular.fromJson(response.body); | ||
|
||
if (apiRes.meta.status === 'SUCCESS') { | ||
products.length = 0; | ||
angular.extend(products, apiRes.payload['ArrayList<Product>']); | ||
defer.resolve(); | ||
ProductRepo.reset(); | ||
defer.notify(); | ||
} else { | ||
console.error(apiRes.meta); | ||
throw "Unable to retrieve products"; | ||
} | ||
}; | ||
|
||
var processRemoteProduct = function (response, productId, deferRefresh) { | ||
var apiRes = angular.fromJson(response.body); | ||
|
||
if (apiRes.meta.status === 'SUCCESS') { | ||
if (angular.isDefined(remoteProducts[productId])) { | ||
for (var key in remoteProducts[productId]) { | ||
remoteProducts[productId][key] = undefined; | ||
} | ||
} else { | ||
remoteProducts[productId] = {}; | ||
} | ||
|
||
angular.extend(remoteProducts[productId], apiRes.payload.HashMap); | ||
deferRefresh.notify(); | ||
} else { | ||
throw "Unable to retrieve remote products for product " + productId; | ||
} | ||
}; | ||
|
||
WsApi.listen(apiMapping.Product.listen).then(null, null, function (response) { | ||
process(response); | ||
}); | ||
|
||
productsService.refreshProducts = function () { | ||
service.refreshProducts = function () { | ||
WsApi.fetch(apiMapping.Product.all).then(function (response) { | ||
process(response); | ||
|
||
for (var productId in remoteProducts) { | ||
service.refreshRemoteProducts(productId); | ||
} | ||
}); | ||
}; | ||
|
||
productsService.getProducts = function () { | ||
return products; | ||
service.refreshRemoteProducts = function (productId) { | ||
var deferRefresh = $q.defer(); | ||
var options = { | ||
pathValues: { | ||
productId: productId | ||
} | ||
}; | ||
|
||
WsApi.fetch(apiMapping.RemoteProducts.byProduct, options).then(function (response) { | ||
processRemoteProduct(response, productId, deferRefresh); | ||
}); | ||
|
||
return deferRefresh.promise; | ||
}; | ||
|
||
service.getRemoteProducts = function () { | ||
return remoteProducts; | ||
}; | ||
|
||
productsService.getProductRemoteProducts = function (productId) { | ||
var remoteProducts = {}; | ||
service.getProducts = function () { | ||
return products; | ||
}; | ||
|
||
service.getRemoteProductInfo = function (productId) { | ||
for (var i in products) { | ||
if (products[i].id == productId) { | ||
return products[i].remoteProducts; | ||
} | ||
} | ||
}; | ||
|
||
productsService.getById = function (id) { | ||
service.getById = function (id) { | ||
return $q(function (resolve, reject) { | ||
productsService.ready.then(function () { | ||
service.ready.then(function () { | ||
for (var i in products) { | ||
if (products[i].id == id) { | ||
resolve(products[i]); | ||
} | ||
} | ||
}); | ||
}.bind(productsService)); | ||
}.bind(service)); | ||
}; | ||
|
||
productsService.refreshProducts(); | ||
service.refreshProducts(); | ||
|
||
productsService.ready = defer.promise; | ||
service.ready = defer.promise; | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.