فهرست منبع

Refactored people.service.dev to use helper functions for finding children and parents

Joe Hawkins 8 سال پیش
والد
کامیت
81f0a3f31a

+ 13 - 12
src/main/angular/src/peoplesearch/orgchart-search.component.ts

@@ -22,20 +22,22 @@ export default class OrgChartSearchComponent {
     }
 
     $onInit(): void {
-        // Retrieve data from the server
         var self = this;
+
         var personId: string = this.$stateParams['personId'];
-        this.fetchOrgChartData(personId).then((orgChartData: OrgChartData) => {
-            // Override personId in case it was undefined
-            personId = orgChartData.self.userKey;
-            this.$q
-                .all({
-                    directReports: this.peopleService.getDirectReports(personId),
-                    managementChain: this.peopleService.getManagementChain(personId),
-                    person: this.peopleService.getPerson(personId)
+
+        this.fetchOrgChartData(personId)
+            .then((orgChartData: OrgChartData) => {
+                // Override personId in case it was undefined
+                personId = orgChartData.self.userKey;
+
+                self.$q.all({
+                    directReports: self.peopleService.getDirectReports(personId),
+                    managementChain: self.peopleService.getManagementChain(personId),
+                    person: self.peopleService.getPerson(personId)
                 })
                 .then((data) => {
-                    this.$scope.$evalAsync(() => {
+                    self.$scope.$evalAsync(() => {
                         self.directReports = data['directReports'];
                         self.managementChain = data['managementChain'];
                         self.person = data['person'];
@@ -44,7 +46,7 @@ export default class OrgChartSearchComponent {
                 .catch((result) => {
                     console.log(result);
                 });
-        });
+            });
     }
 
     autoCompleteSearch(query: string): IPromise<Person[]> {
@@ -57,6 +59,5 @@ export default class OrgChartSearchComponent {
 
     private fetchOrgChartData(personId): IPromise<OrgChartData> {
         return this.peopleService.getOrgChartData(personId);
-
     }
 }

+ 15 - 8
src/main/angular/src/services/people.service.dev.ts

@@ -1,4 +1,4 @@
-import { IPromise, IQService, ITimeoutService } from 'angular';
+import { IPromise, IQService } from 'angular';
 import Person from '../models/person.model';
 import { IPeopleService } from './people.service';
 import OrgChartData from '../models/orgchart-data.model';
@@ -7,11 +7,12 @@ var peopleData = require('./people.data');
 
 export default class PeopleService implements IPeopleService {
     private people: Person[];
-    static $inject = ['$q'];
 
+    static $inject = ['$q'];
     constructor(private $q: IQService) {
         this.people = peopleData.map((person) => new Person(person));
     }
+
     autoComplete(query: string): IPromise<Person[]> {
         return this.search(query)
             .then((people: Person[]) => {
@@ -28,7 +29,7 @@ export default class PeopleService implements IPeopleService {
     }
 
     getDirectReports(id: string): angular.IPromise<Person[]> {
-        var people = this.people.filter((person: Person) => person.detail['manager']['typeMetaData'].userKey == id);
+        var people = this.findDirectReports(id);
 
         return this.$q.resolve(people);
     }
@@ -39,7 +40,7 @@ export default class PeopleService implements IPeopleService {
         if (person) {
             var managementChain: Person[] = [];
 
-            while (person = this.findPerson(person.detail['manager']['typeMetaData'].userKey)) {
+            while (person = this.findManager(person)) {
                 managementChain.push(person);
             }
 
@@ -55,10 +56,8 @@ export default class PeopleService implements IPeopleService {
         }
 
         var self = this.findPerson(personId);
-        var manager = self.detail['manager'];
-        var children = this.people.filter((person: Person) => {
-            return self.detail['manager']['typeMetaData'].userKey == personId;
-        });
+        var manager = this.findManager(self);
+        var children = this.findDirectReports(personId);
 
         var orgChartData = new OrgChartData(manager, children, self);
 
@@ -99,6 +98,14 @@ export default class PeopleService implements IPeopleService {
 
     }
 
+    private findDirectReports(id: string): Person[] {
+        return this.people.filter((person: Person) => person.detail['manager']['typeMetaData'].userKey == id);
+    }
+
+    private findManager(person: Person): Person {
+        return this.findPerson(person.detail['manager']['typeMetaData'].userKey);
+    }
+
     private findPerson(id: string): Person {
         var people = this.people.filter((person: Person) => person.userKey == id);