在我的数据服务中,我有:
//Observables
currentTicket$: Observable<Ticket>;
ticketTypes$: Observable<Array<TicketType>>;
currentTicketSurvey$: Observable<TicketSurvey>;
//observers
private _ticketTypesObserver: any;
private _currentTicketSurveyObserver: any;
private _currentTicketObserver: any;
private _dataStore: {
currentTicket: Ticket,
ticketTypes: Array<TicketType>,
currentTicketSurvey: TicketSurvey
}
constructor(private http: Http){
//set up observables
this.currentTicket$ = new Observable(observer=> this._currentTicketObserver = observer).share();
this.ticketTypes$ = new Observable(observer=> this._ticketTypesObserver = observer).share();
this.currentTicketSurvey$ = new Observable(observer=> this._currentTicketSurveyObserver = observer).share();
//init datastore
this._dataStore = {
currentTicket: null,
ticketTypes: [],
currentTicketSurvey: null
};
}
//Data Retrieval Methods
getTicketTypes() {
if (this._dataStore.ticketTypes.length > 0) //if we already have ticket types, don't refresh. Static Data.
return;
this.http.get('http://localhost:65421/tickets/GetTicketTypes/')
.map(res => (<Response>res).json())
.map((types: Array<any>) => {
let result: Array<TicketType> = [];
if (types) {
types.forEach(ticketType => {
result.push(
new TicketType(
ticketType.Description
, ticketType.Id
, ticketType.IsDisplayed
));
});
}
return result;
}).
subscribe(
data => {
this._dataStore.ticketTypes = data;
this._ticketTypesObserver.next(this._dataStore.ticketTypes);
},
err => {
console.log(err);
}
);
}
getTicketSurvey(ticketId:string) {
this.http.get('http://localhost:65421/tickets/GetTicketById/' + ticketId)
.map(res => (<Response>res).json())
.map((survey: TicketSurvey) => {
let result: TicketSurvey = null;
if (survey) {
result = new TicketSurvey(
survey.id,
survey.ticketId,
survey.ticketTypeId
);
}
return result;
}).
subscribe(
data => {
this._dataStore.currentTicketSurvey = data;
this._currentTicketSurveyObserver.next(this._dataStore.currentTicketSurvey);
},
err => {
console.log(err);
}
);
}
getTicketById(id:string) {
this.http.get('http://localhost:65421/tickets/GetTicketById/' + id)
.map(res => (<Response>res).json())
.map((ticketRes: Ticket) => {
let result: Ticket = null;
if (ticketRes) {
result = new Ticket(
//Set constructor values
);
}
return result;
}).
subscribe(
data => {
this._dataStore.currentTicket = data;
this._currentTicketObserver.next(this._dataStore.currentTicket);
},
err => {
console.log(err);
}
);
}
我的组件有:
currTicket: Ticket;
ticketTypes: Array<TicketType>;
currTicketType: TicketType;
currTicketSurvey: TicketSurvey;
ngOnInit() {
this.ticketDataService.currentTicket$.subscribe(cTicket => this.currTicket = cTicket);
this.ticketDataService.ticketTypes$.subscribe(tTypes => this.ticketTypes = tTypes);
this.ticketDataService.currentTicketSurvey$.subscribe(cSurvey => this.currTicketSurvey = cSurvey);
this.load();
}
load()
{
this.ticketDataService.getTicketById(this._routeParams.get('id'));
this._ticketDataService.getTicketTypes();
this._ticketDataService.getTicketSurvey(this._routeParams.get('id'));
//////************ Need something here to set the current ticket type
//this.ticketTypes.forEach((tType)=>{
// if(tType.id == this.currTicketSurvey.ticketTypeId)
// this.currTicketType = tType;
// return;
//});
}
我遇到的问题是如何在组件中设置currentTicketType?它取决于已加载的currentTicketSurvey和已加载的ticketTypes。这些可能会或可能不会在我的代码的其他区域一起调用,因为它们是异步调用,所以我不能在我在服务上调用它们之后立即设置它。在淘汰赛中我认为我可以设置一个计算的observable并完成它。我找不到Angular 2的等价物。有关如何处理这种情况以便在加载其他对象后设置currentTicketType的任何想法?
肯定会经常出现。我看到的示例相似但不相同使用defer和concatAll,但看起来它会假设这些方法总是以相同的顺序或同时调用。我只是无法将我正在寻找的东西拼凑起来。
答案 0 :(得分:2)
RxJS提供了一组combining operators,我认为Observable.forkJoin
或Observable.merge
可以满足您的需求。
Observable.forkJoin
将等待一组可观察事件接收到事件(每个事件):
Observable.forkJoin([a,b]).subscribe(t=> {
var firstResult = t[0];
var secondResult = t[1];
});
Observable.merge
。当收到2个结果时,流将关闭
Observable.merge(obs1, obs2)
.take(2)
.subscribe({complete: () => { ... });
这个问题可以给你一些额外的提示: