Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

How to declare simple observer with function #2436

Merged
3 commits merged into from
Jan 12, 2018
Merged

How to declare simple observer with function #2436

3 commits merged into from
Jan 12, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jan 11, 2018

Fixes #2404

@ghost
Copy link
Author

ghost commented Jan 11, 2018

@arthurevans and/or @TimvdLippe can has sanity-check?

@ghost ghost requested review from arthurevans and TimvdLippe January 11, 2018 22:30
Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Just a question on a potentially nicer notation of the function declaration, but it is mostly a nit 😛

observer:
function(newValue, oldValue){
this.toggleClass('highlight', newValue);
}.bind(this)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a lambda reads clearer. Does the following work?

observer(newValue, oldValue) {
  this.toggleClass('highlight', newValue);
}

Copy link
Author

@ghost ghost Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this?

observer: 
  function observer(newValue, oldValue) {
    this.toggleClass('highlight', newValue);
  }

Or ...?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this:

static get properties() {
  return {
    active: {
      type: Boolean,
      // Observer method
      observer(newValue, oldValue) {
          this.toggleClass('highlight', newValue);
      }
  }
}

A running example: http://jsbin.com/xisifaboso/edit?html,console,output This is using the "object method shorthand definition" in ES2015: https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Functions/Method_definitions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, didn't know that was a thing XD

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it is fairly new and neatly concise 😄 This PR now LGTM fully!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@ghost ghost merged commit a636885 into master Jan 12, 2018
@ghost ghost deleted the observer-functions branch February 20, 2018 19:13
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants