How do I set a property in a polymer-dart behavior mixin?

Issue

The following example is taken from the polymer-dart documentation on behaviors. It makes use of the method set in toggleHighlight. I don’t understand how this is possible since set isn’t defined anywhere.

@behavior
abstract class HighlightBehavior {
  @Property(notify: true, observer: 'highlightChanged')
  bool isHighlighted = false;

  static created(instance) {
    print('Highlighting for $instance enabled!');
  }

  @Listen('click')
  toggleHighlight(_, __) {
    set('isHighlighted', !isHighlighted);
  },

  @reflectable
  highlightChanged(bool newValue, _) {
    toggleClass('highlighted', newValue);
  }
}

How do I set a polymer property in a behavior that triggers all the functionality that makes data binding work?

Should a behavior possibly implement PolymerBase to be able to use the set-method? A quick test reveals that set works when the behavior implements PolymerBase. But this is not how it is documented. May I induce some unwanted side-effects by implementing PolymerBase?

Solution

The HighlightBehavior is abstract, so real instances are obtained with inheritance. From the documentation

class MyElement extends PolymerElement with HighlightBehavior {
  MyElement.created() : super.created();
}

The PolymerElement extends PolymerBase which supply the set method.

Answered By – Jonas Bojesen

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.