How to use Google Dart web-ui @observable separately?

I cannot figure out how to use the @observable annotation / Observable class to receive simple notifications when the state of an object changes.

import 'package:web_ui/observe.dart';

@observable class T{
  String x;
// what else is needed?
}

T t = new T();
observe(t, (e) => print ("Value changed"));
t.x = "Changed";

I would like to use observables without the rest of web-ui, if possible (as a replacement for backbone.js).

+5
source share
2 answers

You will need to run the dwc compiler, which searches for @observable and generates new source code that actually implements the observation. I have never tried to run observables without a web interface, but you will definitely need dwc to create the correct output.

+2
source

, . , @observable WebUI/Polymer. . :

import 'package:observe/observe.dart';

void main() {
  var person = new Person(18, false);

  person.changes.listen((List<ChangeRecord> changes) {
    changes.forEach((PropertyChangeRecord change) {
      print("$change");
    });
  });

  person.changes.listen((List<ChangeRecord> changes) {
    changes.where((PropertyChangeRecord c) => c.name == #employed).forEach((PropertyChangeRecord change) {
      print("Employment status changed!");
    });
  });

  print("start changing");

  person.age =  19;
  person.age =  19;
  person.age =  19;
  person.age = 20;
  person.employed = true;
  person.age = 21;
  person.age = 22;

  print("finish changing");
}

class Person extends ChangeNotifier {

  int _age;
  int get age => _age;
  void set age (int val) {
    _age = notifyPropertyChange(#age, _age, val);
  }

  bool _employed;
  bool get employed => _employed;
  void set employed (bool val) {
    _employed = notifyPropertyChange(#employed, _employed, val);
  }

  Person(this._age, this._employed);
}

, , .

start changing
finish changing
#<PropertyChangeRecord Symbol("age") from: 18 to: 19>
#<PropertyChangeRecord Symbol("age") from: 19 to: 20>
#<PropertyChangeRecord Symbol("employed") from: false to: true>
#<PropertyChangeRecord Symbol("age") from: 20 to: 21>
#<PropertyChangeRecord Symbol("age") from: 21 to: 22>
Employment status changed!
0

All Articles