2016-02-01 1 views
1

Мне нужно изменить свой код при загрузке подробной категории, прежде всего, если он еще не загружен в инструкцию, и если не так подробно загружается. Спасибо за помощь!Обнаружить, если Observable не найден с использованием combLatest

Конструктор 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; 
     }); 
} 

CategoryList:

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)); 
     }); 
} 

CategoryDetail:

loadCategory(categoryId:number){ 

    this._obServers.idCategory.next(categoryId); 

    //If category not loaded I need to load it 

} 

ответ

1

Я следовал этому пути 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));