How to subscribe to change the observed field

until recently, I could use bindProperty, as shown below or in this question , but this has changed with 0.8.0, and I don’t know how to change my code to get the old behavior (it calls doSomething ()):

<polymer-element name="my-login" attributes="model"> <template> <template if="{{"model.isLoggedIn}}"> ... </template> </template> <script type= ... ></script> </polymer-element> 

.

 @CustomTag("my-login") class MyLogin extends PolymerElement with ObservableMixin { LoginModel model; @override inserted() { void doSomething() { ... } 

logoutChangeSubscription = bindProperty (model, #isLoggedIn, () => doSomething ());

  } } class Model extends Object with ObservableMixin { @observable bool isLoggedIn = false; } 
+6
source share
3 answers

With Polymer.dart 0.8 or higher, you can also use this convenient form:

 isLoggedInChanged(oldValue) { doSomething(); } 

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

Here is 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; } } 
+6
source

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 him

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

The syntax seems to have changed a bit. The syntax for the solution proposed by Gunther now looks like this:

 new PathObserver(model, "isLoggedIn").open((e) => doSomething()); 
+1
source

Source: https://habr.com/ru/post/955255/


All Articles