You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -566,6 +567,124 @@ Many of my styles have been from the many pair programming sessions [Ward Bell](
566
567
567
568
**[Back to top](#table-of-contents)**
568
569
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.
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
+
functionAvengers(dataservice, logger) {
623
+
var vm =this;
624
+
vm.avengers= [];
625
+
626
+
activate();
627
+
628
+
functionactivate() {
629
+
returngetAvengers().then(function() {
630
+
logger.info('Activated Avengers View');
631
+
});
632
+
}
633
+
634
+
functiongetAvengers() {
635
+
returndataservice.getAvengers()
636
+
.then(function (data) {
637
+
vm.avengers= data;
638
+
returnvm.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
+
functionactivate() {
654
+
/**
655
+
* Step 1
656
+
* Ask the getAvengers function for the
657
+
* avenger data and wait for the promise
658
+
*/
659
+
returngetAvengers().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
+
functiongetAvengers() {
669
+
/**
670
+
* Step 2
671
+
* Ask the data service for the data and wait
672
+
* for the promise
673
+
*/
674
+
returndataservice.getAvengers()
675
+
.then(function (data) {
676
+
/**
677
+
* Step 3
678
+
* set the data and resolve the promise
679
+
*/
680
+
vm.avengers= data;
681
+
returnvm.avengers;
682
+
});
683
+
}
684
+
```
685
+
686
+
**[Back to top](#table-of-contents)**
687
+
569
688
## Directives
570
689
- **Limit 1 Per File**: Create one directive per file. Name the file for the directive.
0 commit comments