Skip to content

Commit

Permalink
add battle service
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelaz3r committed Nov 7, 2016
1 parent 82bcdfa commit fb7f121
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import { Signin } from '../pages/signin/signin';
import { TabsPage } from '../pages/tabs/tabs';
import { BattlesService } from '../providers/battles-service';

@NgModule({
declarations: [
Expand All @@ -25,6 +26,8 @@ import { TabsPage } from '../pages/tabs/tabs';
Signin,
TabsPage
],
providers: []
providers: [
BattlesService
]
})
export class AppModule {}
3 changes: 3 additions & 0 deletions src/models/battle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Battle {
id: string;
}
18 changes: 15 additions & 3 deletions src/pages/home/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

import { Battle } from '../../models/battle';
import { BattlesService } from '../../providers/battles-service';

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public battles: Battle[];

constructor(public navCtrl: NavController) {

constructor(public navCtrl: NavController, public battlesService: BattlesService) {
this.load();
}

}
load() {
this.battlesService.load()
.subscribe((data) => {
this.battles = data
console.log(data);
});

}
}
40 changes: 40 additions & 0 deletions src/providers/battles-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';

import { Battle } from '../models/battle';

const API = 'http://86.119.36.241:3000/';
const HEAD = new Headers({
'Content-Type': 'application/json'
// 'Access-Control-Allow-Origin': '*'
});

@Injectable()
export class BattlesService {
constructor(public http: Http) {
console.log('Hello BattlesService Provider');
}

load(): Observable<Battle[]> {
return this.http.get(`${API}/battles`, {headers: HEAD})
.map(res => {
console.log(res);
})
/* .map(res => res.json().map(
res => {
let b = new Battle();
b.id = res.id;
return b;
}
))*/
.catch(this.handleError);
}

private handleError(error) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}

0 comments on commit fb7f121

Please sign in to comment.