Flex-layout: Update wiki to reflect change for MediaObserver made in beta 24

Created on 19 Mar 2019  路  6Comments  路  Source: angular/flex-layout

Bug Report

What is the expected behavior?

To wiki show the correct way to be used now.

What is the current behavior?

I updated to latest beta 24, and saw that $media got deprecated, but docs still shows the old way for listening for MediaChange (using it).
I couldn't find a way to get the asObservable from MediaObserver that is not from $media like it shows in changelog.
What is the proper way to do it? The wiki needs to be updated to reflect this change

P4 docs help wanted

Most helpful comment

While media$ is an Observable\

this.watcher = mediaObserver.media$.subscribe((change: MediaChange) => {
  this.activeMediaQuery = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
  if ( change.mqAlias == 'xs') {
     this.loadMobileContent();
  }
});

becomes:

this.watcher = mediaObserver.asObservable()
  .pipe(
    filter((changes: MediaChange[]) => changes.length > 0),
    map((changes: MediaChange[]) => changes[0])
  ).subscribe((change: MediaChange) => {
    this.activeMediaQuery = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
    if ( change.mqAlias == 'xs') {
      this.loadMobileContent();
    }
  });

Obviously there are many ways to handle it, the big change is you now get an array of changes

All 6 comments

@CaerusKaru I saw in another issue that you are rebuilding the docs. In the meanwhile, can you post here how to solve this issue that I'm facing?

While media$ is an Observable\

this.watcher = mediaObserver.media$.subscribe((change: MediaChange) => {
  this.activeMediaQuery = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
  if ( change.mqAlias == 'xs') {
     this.loadMobileContent();
  }
});

becomes:

this.watcher = mediaObserver.asObservable()
  .pipe(
    filter((changes: MediaChange[]) => changes.length > 0),
    map((changes: MediaChange[]) => changes[0])
  ).subscribe((change: MediaChange) => {
    this.activeMediaQuery = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
    if ( change.mqAlias == 'xs') {
      this.loadMobileContent();
    }
  });

Obviously there are many ways to handle it, the big change is you now get an array of changes

@jwhollingsworth Forgive me, but why would mediaObserver publish events where the value would be an empty array? Do we really need to filter for that condition?

@chriszrc If I remember correctly, that was just how the source code at the time mapped between the two. It had the filter, and I just copy and pasted it. No clue what use case that handles other than being extra careful.

I believe that Wiki is now updated.
For the ones still using an older version (7.0.0-beta.19 in my case),

import { MediaObserver } from "@angular/flex-layout";

will throw an error like module has not such exported member

you'll need to use

import { MediaService } from "@angular/flex-layout";
// ...
mediaObserver.media$
.pipe(takeUntil(this._subCtrl$))
.subscribe((change: MediaChange) => {
 // mqAlias contains xs, sm, md, lg
  if ( change.mqAlias == "xs") {
     // DO STUFF
  }
});

@tonysamperi The documentation is still not updated (https://github.com/angular/flex-layout/wiki/API-Documentation) and shows how to use the deprecated media$.subscribe. Your example also does not answer the original poster's request on how to use the asObservable() that the deprecation statement says to use.

Was this page helpful?
0 / 5 - 0 ratings