Skip to content

Commit 994857c

Browse files
committed
added data service abstraction and $http calls and promises. closes johnpapa#38
1 parent 916ae53 commit 994857c

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Many of my styles have been from the many pair programming sessions [Ward Bell](
1919
1. [Controllers](#controllers)
2020
1. [Services](#services)
2121
1. [Factories](#factories)
22+
1. [Data Services](#data-services)
2223
1. [Directives](#directives)
2324
1. [Resolving Promises for a Controller](#resolving-promises-for-a-controller)
2425
1. [Manual Dependency Injection](#manual-dependency-injection)
@@ -566,6 +567,124 @@ Many of my styles have been from the many pair programming sessions [Ward Bell](
566567
567568
**[Back to top](#table-of-contents)**
568569
570+
## Data Services
571+
572+
- **Separate Data Calls**: Refactor logic for making data operations and interacting with data to a factory. Make data services responsible for XHR calls, local storage, stashing in memory, or any other data operations.
573+
574+
*Why?*: The controller's responsibility is for the presentation and gathering of information for the view. It should not care ow it gets the data, just that it knows who to ask for it. Separating the data services moves the logic on how to get it to the data service, and let's the controller be simpler and more focused on the view.
575+
576+
*Why?*: This makes it easier to test (mock or real) the data calls when testing a controller that uses a data service.
577+
578+
*Why?*: Data service implementation may have very specific code to handle the data repository. This may include headers, how to talk to the data, or other services such as $http. Separating the logic into a data service encapsulates this logic in a single place hiding the implementation from the outside consumers (perhaps a controller), also making it easier to change the implementation.
579+
580+
```javascript
581+
/* recommended */
582+
583+
// dataservice factory
584+
angular
585+
.module('app.core')
586+
.factory('dataservice', dataservice);
587+
588+
dataservice.$inject = ['$http', 'logger'];
589+
590+
function dataservice($http, logger) {
591+
return {
592+
getAvengers: getAvengers
593+
};
594+
595+
function getAvengers() {
596+
return $http.get('/api/maa')
597+
.then(getAvengersComplete)
598+
.catch(getAvengersFailed);
599+
600+
function getAvengersComplete(data, status, headers, config) {
601+
return data.results;
602+
}
603+
604+
function getAvengersFailed(error) {
605+
logger.error('XHR Failed for getAvengers.' + error.data);
606+
}
607+
}
608+
}
609+
```
610+
- Note: The data service is called from consumers, such as a controller, hiding the implementation from the consumers, as shown below.
611+
612+
```javascript
613+
/* recommended */
614+
615+
// controller calling the dataservice factory
616+
angular
617+
.module('app.avengers')
618+
.controller('Avengers', Avengers);
619+
620+
Avengers.$inject = ['dataservice', 'logger'];
621+
622+
function Avengers(dataservice, logger) {
623+
var vm = this;
624+
vm.avengers = [];
625+
626+
activate();
627+
628+
function activate() {
629+
return getAvengers().then(function() {
630+
logger.info('Activated Avengers View');
631+
});
632+
}
633+
634+
function getAvengers() {
635+
return dataservice.getAvengers()
636+
.then(function (data) {
637+
vm.avengers = data;
638+
return vm.avengers;
639+
});
640+
}
641+
}
642+
```
643+
644+
- **Return a Promise from Data Calls**: When calling a data service that returns a promise such as $http, return a promise in your calling function too.
645+
646+
*Why?*: You can chain the promises together and take further action after the data call completes and resolves or rejects the promise.
647+
648+
```javascript
649+
/* recommended */
650+
651+
activate();
652+
653+
function activate() {
654+
/**
655+
* Step 1
656+
* Ask the getAvengers function for the
657+
* avenger data and wait for the promise
658+
*/
659+
return getAvengers().then(function() {
660+
/**
661+
* Step 4
662+
* Perform an action on resolve of final promise
663+
*/
664+
logger.info('Activated Avengers View');
665+
});
666+
}
667+
668+
function getAvengers() {
669+
/**
670+
* Step 2
671+
* Ask the data service for the data and wait
672+
* for the promise
673+
*/
674+
return dataservice.getAvengers()
675+
.then(function (data) {
676+
/**
677+
* Step 3
678+
* set the data and resolve the promise
679+
*/
680+
vm.avengers = data;
681+
return vm.avengers;
682+
});
683+
}
684+
```
685+
686+
**[Back to top](#table-of-contents)**
687+
569688
## Directives
570689
- **Limit 1 Per File**: Create one directive per file. Name the file for the directive.
571690

0 commit comments

Comments
 (0)