I am trying to use a service to define a shared resource client between several views. I first select the client from an index and set it:
itemTapped(event, client) {
this.clientService.get(`/${client.id}?model=client`, this.current_user.auth_token)
.subscribe(
data => {
this.clientService.setClient(data)
this.navCtrl.push(ClientGenericPage, {
client_id: data['id'],
tab: 'profile',
current_user: this.current_user
});
},
err => {
this.clientService.handleResponse("Ut oh.. Could not locate the client !"),
this.clientService.hideLoader()
},
() => this.clientService.hideLoader()
)
}
export class ClientService {
private client = new Subject<any>();
constructor (private http: Http, private toastCtrl: ToastController, public storage: Storage ) {
}
setClient(client) {
this.client.next(client)
}
getClient() {
return this.client.asObservable();
}
get(url, token):Observable<Response> {
let headers = new Headers();
headers.append("authentication", token );
let options = new RequestOptions({ headers: headers });
let final_url = `${this.baseUrl}${url}`
return this.http.get(final_url, options)
.map(this.extractData)
.catch(this.handleError)
}
}
Then I try to get the client that was just set and subscribe to it so that I can detect changes to client in this view and its siblings:
this.clientService.getClient()
.subscribe(
val => {
console.log(val)
this.client = val
},
err => console.log(err),
() => {}
)
but this call to getClient() never returns an error or anything at all. What am I doing wrong with my observable?
EDIT: Here is the component:
@Component({
selector: 'page-client-details',
templateUrl: 'client-details.html',
providers: [FilterObject, ClientService]
})
export class ClientDetailsPage {
client:any;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private filterObject: FilterObject,
public http: Http,
public modalController: ModalController,
private clientService: ClientService,
private app: App
) {
this.clientService.getClient()
.subscribe(
val => {
console.log(val)
this.client = val
},
err => console.log(err),
() => console.log("completed")
)
}
this.clientService.getClient()is calledval => this.client = valgetClient()and set it tothis.client() => {}to() => { console.log("completed") }and never see that in the console