[apiService] created a service for getting the API URL
authorErik Haider Forsén <erik@forsen.not>
Sat, 14 Nov 2015 12:43:58 +0000 (13:43 +0100)
committerErik Haider Forsén <erik@forsen.not>
Sat, 14 Nov 2015 12:43:58 +0000 (13:43 +0100)
src/components/app.ts
src/components/dhisapi/dhisapiService.ts [new file with mode: 0644]
src/components/filter/filter.ts
src/index.html

index cb9479e..a3d4fd3 100644 (file)
@@ -3,6 +3,7 @@ import {Component, View, bootstrap, provide} from 'angular2/angular2';
 import {Map} from './map/map';
 import {Search} from "./search/search";
 import {Filter} from "./filter/filter";
+import {DhisapiService} from "./dhisapi/dhisapiService";
 
 declare var System:any;
 
@@ -10,12 +11,18 @@ declare var System:any;
     {
         selector: 'mou-app',
         templateUrl: './components/app.html',
-        directives:[Filter, Map, Search]
+        directives:[Filter, Map, Search],
+        providers: [DhisapiService]
     })
 
 
 class App {
+    constructor(dhis: DhisapiService){
+        dhis.getApiURL(function(uri){
+            console.log(uri);
+        })
+    }
 
 }
 
-bootstrap(App,[HTTP_PROVIDERS]);
\ No newline at end of file
+bootstrap(App,[HTTP_PROVIDERS,DhisapiService]);
diff --git a/src/components/dhisapi/dhisapiService.ts b/src/components/dhisapi/dhisapiService.ts
new file mode 100644 (file)
index 0000000..38f9c4b
--- /dev/null
@@ -0,0 +1,18 @@
+import {Injectable} from 'angular2/angular2';
+import {Http} from 'angular2/http';
+
+@Injectable()
+export class DhisapiService {
+    private http: Http;
+
+    constructor(http: Http){
+        this.http = http;
+    }
+
+    getApiURL = function(callback){
+        this.http.get('../manifest.webapp')
+            .map(res => res.json())
+            .map(res => res.activities.dhis.href)
+            .subscribe( res => callback(res));
+    }
+}
index a3a63b7..9181b79 100644 (file)
@@ -1,15 +1,18 @@
 import {Component, View, CORE_DIRECTIVES} from 'angular2/angular2';
 import {Headers, Http} from 'angular2/http';
+import {DhisapiService} from "../dhisapi/dhisapiService";
 
 @Component({
     selector: 'mou-filter',
     directives: [CORE_DIRECTIVES],
-    templateUrl: './components/filter/filter.html'
+    templateUrl: './components/filter/filter.html',
+    providers: [DhisapiService]
 })
 
 export class Filter {
     result: Object;
 
+    dhis: DhisapiService;
     // Example HTTP request
 
     /*
@@ -31,4 +34,15 @@ export class Filter {
     }
 
     */
+
+    constructor(dhis: DhisapiService){
+        this.dhis = dhis;
+    }
+
+    onInit(){
+        this.dhis.getApiURL(function(uri){
+            console.log("Dette er uri: " + uri);
+        })
+    }
+
 }
index c712323..3b1e667 100644 (file)
@@ -3,13 +3,7 @@
 <head>
     <meta charset="UTF-8">
     <title>Sample App</title>
-    <script>
-        var xhReq = new XMLHttpRequest();
-        xhReq.open("GET", "../manifest.webapp", false);
-        xhReq.send(null);
-        var serverResponse = JSON.parse(xhReq.responseText);
-        var dhisAPI = serverResponse.activities.dhis.href;
-    </script>
+
     <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDlgw6kZAQvD8ex_md-6wb_NcnVlZWkbE4"></script>
     <script src="../node_modules/systemjs/dist/system.src.js"></script>
     <script src="../node_modules/angular2/bundles/angular2.dev.js"></script>