findOne().zone()


#1

Hi!

I use Meteor + Angular 2, and have a one question. To connect collection changes into view using Component’s Zone:

const Companies = new MongoObservable.Collection<Company>('companies')

constructor() {
   this.companies = Companies.find({}).zone();
}

And what if I need to select only one company? How to use findOne() method with zone()?


#2

Hi

Maybe you could try to track for changes with Tracker.autorun and NgZone.
Not sure it is the best practice, but it works for me

import { Component, NgZone, OnInit, OnDestroy } from '@angular/core';
import { Tracker } from 'meteor/tracker';

.../...

export class companiesComponent implements OnInit, OnDestroy {
  companies: Companies;
  companyName: string;

  constructor(private zone: NgZone) {}

  ngOnInit() {
    this.companies = MeteorObservable.subscribe('companies-publication', this.joueurName).subscribe(
      () => {
        Tracker.autorun(() => {
          this.zone.run(() => {
            this.companyInformations = CompaniesCollection.findOne({ name: this.companyName });
          });
        });
      }
    );
  }

.../...

Tracker.autorun allows you to run a function that depends on reactive data sources, in such a way that if there are changes to the data later, the function will be rerun: