Search code examples
dartdart-polymer

Dart: @observable of getters


I am trying to understand how observable getters work when they use other class instance properties:

When I bind the implicit getter/setter pair 'name' it updates in the input and in the div and everything is synced nicely.

However the explicit getter 'fullname' is not updating in the HTML. Is there a way to make that work (basically the 'fullname' in the element binding should update as well)?? Maybe I am missing a setter, but then again setter does not make sense here...

Very simple example to demonstrate:

test-element.html

<link rel="import" href="../../packages/polymer/polymer.html">
<polymer-element name="test-element">
  <template>
    <input value="{{ds.name}}">
    <div>{{ds.name}}</div>
    <div>{{ds.fullname}}</div>
  </template>
  <script type="application/dart" src="test1.dart"></script>
</polymer-element>

test-element.dart

import 'package:polymer/polymer.dart';
import 'package:popoli/sysmaster-settings.dart';

@CustomTag('test-element')
class TestElement extends PolymerElement {
  @observable VerySimpleTest ds;

  TestElement.created() : super.created() {
    ds = new VerySimpleTest()..name = 'Peter';
  }
}

ds.dart

class VerySimpleTest extends Observable {
  @observable String name = '';
  @observable String get fullname => 'Test: $name';
  VerySimpleTest() : super();
}

Solution

  • You need to notify Polymer that a value has changed the getter depends on.

    String set name(String val) {
      name = notifyPropertyChange(#fullname, name, val);
    }
    

    or this should work too

    @ComputedProperty('Test: $name') String get fullname => 'Test: $name';
    

    See http://japhr.blogspot.co.at/2014/08/the-polymerdart-computedproperty.html for more details.