在初始加载

时间:2017-02-02 17:08:50

标签: javascript angular asynchronous rxjs contentful

我正在使用contentful js SDK获取服务中的数据。 SDK提供了一种从promise中检索条目并解析这些条目的方法。由于我想使用observable,我将这个承诺作为一个可观察的东西返回,然后从那里转变。

在我的主页组件中,我随后调用 contentfulService OnInit并使用 async 管道展开模板中的observable。

我的问题:
加载主组件时,即使服务已成功获取数据,模板也不存在。现在,如果我与页面上的DOM(点击,悬停)进行交互,模板将立即显示。为什么这不只是在页面加载时异步加载?我该如何解决这个问题?

An example .gif showing the behavior.

contentful.service.ts

import { Injectable } from '@angular/core';    
import { Observable, Subject } from 'rxjs/Rx';    
import { Service } from '../models/service.model';
import * as contentful from 'contentful';


@Injectable()
export class ContentfulService {

  client: any;

  services: Service[];
  service: Service;    

  constructor() {    
    this.client = contentful.createClient({
      space: SPACE_ID,
      accessToken: API_KEY
    });
  }   


  loadServiceEntries(): Observable<Service[]> {

    let contentType = 'service';
    let selectParams = 'fields';

    return this.getEntriesByContentType(contentType, selectParams)
      .take(1)          
      .map(entries => {
        this.services = [];

        let parsedEntries = this.parseEntries(entries);

        parsedEntries.items.forEach(entry => {
          this.service = entry.fields;
          this.services.push(this.service);
        });

        this.sortAlpha(this.services, 'serviceTitle');
        return this.services;
      })          
      .publishReplay(1)
      .refCount();

  }


  parseEntries(data) {
    return this.client.parseEntries(data);
  }


  getEntriesByContentType(contentType, selectParam) {

    const subject = new Subject();

    this.client.getEntries({
      'content_type': contentType,
      'select': selectParam
    })
      .then(
      data => {
        subject.next(data);
        subject.complete();
      },
      err => {
        subject.error(err);
        subject.complete();
      }
      );

    return subject.asObservable();
  }


  sortAlpha(objArray: Array<any>, property: string) {
    objArray.sort(function (a, b) {
      let textA = a[property].toUpperCase();
      let textB = b[property].toUpperCase();

      return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
    });
  }


}

home.component.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { ContentfulService } from '../shared/services/contentful.service';
import { Service } from '../shared/models/service.model';    

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  service: Service;      
  services: Service[];
  services$: Observable<Service[]>;

  constructor(
    private contentfulService: ContentfulService,
  ) {

  }    

  ngOnInit() {       

    this.services$ = this.contentfulService.loadServiceEntries();

    this.services$.subscribe(
      () => console.log('services loaded'),
      console.error
    );    

  }; 


}

home.component.html

...
<section class="bg-faded">
  <div class="container">
    <div class="row">
      <div class="card-deck">
        <div class="col-md-4 mb-4" *ngFor="let service of services$ | async">
          <div class="card card-inverse text-center">
            <img class="card-img-top img-fluid" [src]="service?.serviceImage?.fields?.file?.url | safeUrl">
            <div class="card-block">
              <h4 class="card-title">{{service?.serviceTitle}}</h4>
              <ul class="list-group list-group-flush">
                <li class="list-group-item bg-brand-black"><i class="fa fa-wrench mr-2" aria-hidden="true"></i>Cras justo odio</li>
                <li class="list-group-item bg-brand-black"><i class="fa fa-wrench mr-2" aria-hidden="true"></i>Dapibus ac facilisis in</li>
                <li class="list-group-item bg-brand-black"><i class="fa fa-wrench mr-2" aria-hidden="true"></i>Vestibulum at eros</li>
              </ul>
            </div>
            <div class="card-footer">
              <a href="#" class="btn btn-brand-red">Learn More</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
...

1 个答案:

答案 0 :(得分:0)

听起来好的承诺正在Angular的区域之外解决。

您可以通过向服务中注入NgZone来确保在区域内运行可观察的方法:

import { NgZone } from '@angular/core';

constructor(private zone: NgZone) {
  this.client = contentful.createClient({
    space: SPACE_ID,
    accessToken: API_KEY
  });
}

在调用主题方法时,使用注入的区域run进行调用:

getEntriesByContentType(contentType, selectParam) {

  const subject = new Subject();

  this.client.getEntries({
    'content_type': contentType,
    'select': selectParam
  })
  .then(
    data => {
      this.zone.run(() => {
        subject.next(data);
        subject.complete();
      });
    },
    err => {
      this.zone.run(() => {
        subject.error(err);
        subject.complete();
      });
    }
  );

  return subject.asObservable();
}