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

from-event element modifier #4

Merged
merged 2 commits into from
Feb 14, 2019
Merged

from-event element modifier #4

merged 2 commits into from
Feb 14, 2019

Conversation

alexlafroscia
Copy link
Owner

@alexlafroscia alexlafroscia commented Jan 29, 2019

This implements an "element modifier" that can be given an event to listen to an observer that should subscribe to those events

class Foo extends Component {
  clicks = 0;

  handleClick() {
    this.clicks++;
  }
}
<button {{from-event 'click' this.handleClick}}>
  My Button
</button>

You can also pass an optional operator to transform the original observer before subscribing

import { filter } from 'rxjs/operators';

class Bar extends Foo {
  everyOtherClick() {
    return filter((event, index) => index % 2 === 0)
  }
}
<button {{from-event 'click' this.everyOtherClick this.handleClick}}>
  My Button
</button>

And, because of the way that Rx.Subject works, you can also get an observable from the handler if you want to do something specific with it

import { Subject } from 'rxjs';

class Bar extends Foo {
  subject = new Subject();

  init() {
    super.init(...arguments);

    this.subject.subscribe(event => {
      console.log(event);
    });
  }
}
<button {{from-event 'click' this.subject}}>
  My Button
</button>

@alexlafroscia alexlafroscia changed the title From event modifier from-event element modifier Jan 30, 2019
@alexlafroscia
Copy link
Owner Author

PR blocked by something strange going on with ember-modifier-manager-polyfill in CI

ember-polyfills/ember-modifier-manager-polyfill#4

@alexlafroscia alexlafroscia merged commit 744d585 into master Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant