The Wayback Machine - https://web.archive.org/web/20200527132158/https://github.com/angular/flex-layout/issues/1040
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update wiki to reflect change for MediaObserver made in beta 24 #1040

Open
Gustav0ar opened this issue Mar 19, 2019 · 6 comments
Open

Update wiki to reflect change for MediaObserver made in beta 24 #1040

Gustav0ar opened this issue Mar 19, 2019 · 6 comments
Assignees
Milestone

Comments

@Gustav0ar
Copy link

@Gustav0ar Gustav0ar commented Mar 19, 2019

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

@Gustav0ar
Copy link
Author

@Gustav0ar Gustav0ar commented Mar 20, 2019

@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?

@jwhollingsworth
Copy link

@jwhollingsworth jwhollingsworth commented Mar 20, 2019

While media$ is an Observable<MediaChange>, asObservable returns Observable<MediaChange[]>. In the source, media$ is mapped to the first item in the MediaChange array, so you could optionally, do the exact same thing:

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

@chriszrc
Copy link

@chriszrc chriszrc commented Jul 18, 2019

@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?

@jwhollingsworth
Copy link

@jwhollingsworth jwhollingsworth commented Jul 18, 2019

@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.

@tonysamperi
Copy link

@tonysamperi tonysamperi commented Jan 31, 2020

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
  }
});
@jptinsman
Copy link

@jptinsman jptinsman commented Feb 17, 2020

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
7 participants
You can’t perform that action at this time.