簡體   English   中英

使用CombineLatest檢測是否未找到Observable

[英]Detect if Observable not found using combineLatest

我需要修改代碼,在該代碼中,加載詳細信息類別將首先查看該語句是否尚未加載,如果未加載,則將詳細信息加載。 感謝幫助!

CategoryProvider的構造方法:

private _obServers = {
    'categoryList': undefined,
    'category': undefined,
    'idCategory': new Subject<Number>()
};

constructor(){
    this.categories = new Observable(observer => this._obServers.categoryList = observer).share();

    this._categoryObservable = this.categories
        .combineLatest(this._obServers.idCategory, (categories, idCategory) => {
            return categories.filter(category => category.id === idCategory)[0];
        })
        .distinctUntilChanged((oldCategory, newCategory) => {
            return oldCategory.id === newCategory.id;
        });
}

所屬分類:

loadCategories(search?:string):void{
    this._http
        .get('/services/category/list?search=' + search)
        .map(res => res.json())
        .subscribe(data => {
            this._obServers.categoryList.next(this.createCategoryEntities(data));
        });
}

CategoryDe​​tail:

loadCategory(categoryId:number){

    this._obServers.idCategory.next(categoryId);

    //If category not loaded I need to load it

}

我已經按照這種方式https://github.com/JonatanSCS/Angular-2-Tutorial/blob/master/node_modules/rxjs/src/add/observable/combineLatest.ts

import { Component, Injectable, Inject, provide } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { combineLatestStatic } from 'rxjs/operator/combineLatest.js';

import { MessageApi } from '../providers/lb-service/lb-services.provider'
import { EstimatesService } from './estimates.service';


@Component({
    pipes: [TranslatePipe],
})

@Injectable()
export class InvoiceService {

    constructor(private _message:MessageApi,
                @Inject(EstimatesService) _estimates:EstimatesService) {
        this._message = _message;
        this._estimates = _estimates;

        Observable.combineLatest = combineLatestStatic;

        declare module 'rxjs/Observable' {
            namespace Observable {
                export let combineLatest: typeof combineLatestStatic;
            }
        }

        Observable.combineLatest(
            this._estimates.getEstimates(),
            this._message.findOne({
                    where: {
                        moduleTag: 'monthlyStat',
                        'dynamic.date': "2016-07-01" //new Date
                    },
                    fields: {
                        dynamic: true
                    }
                }),this._message.findOne({
                where: {
                    moduleTag: 'areaPriceSE1',
                    'dynamic.date': ''
                },
                fields: {
                    dynamic: true
                }
            })
        ).subscribe(res => console.log("observable", res));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM