How to subscribe to change of an observable field

送分小仙女□ 提交于 2019-12-05 08:03:21
Seth Ladd

With Polymer.dart 0.8 or greater, you can also use this convenience form:

isLoggedInChanged(oldValue) {
  doSomething();
}

Notice how you can create a method inside your PolymerElement that uses a name of yourFieldName*Changed

There's also onPropertyChange as defined here: http://api.dartlang.org/docs/bleeding_edge/observe.html#onPropertyChange

From the docs:

class MyModel extends ObservableBase {
  StreamSubscription _sub;
  MyOtherModel _otherModel;

  MyModel() {
    ...
    _sub = onPropertyChange(_otherModel, const Symbol('value'),
        () => notifyProperty(this, const Symbol('prop'));
  }

  String get prop => _otherModel.value;
  set prop(String value) { _otherModel.value = value; }
}

Polymer.dart >= 1.0.0

@Property(observer: 'doSomething') bool isLoggedIn;

@reflectable
void doSomething(bool newValue, bool oldValue) => ...

or

@Observe('isLoggedIn') 
void doSomething(event, detail) => ...

Polymer.dart < 1.0.0

Ok, found it

new PathObserver(model, "isLoggedIn").changes.listen((e) => doSomething());
Benjamin

It seems that the syntax has changed a little bit. The syntax of the solution proposed by Günter now seems to be:

new PathObserver(model, "isLoggedIn").open((e) => doSomething());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!