如何订阅可观察字段的更改 [英] How to subscribe to change of an observable field
问题描述
直到最近我可以使用bindProperty,如下所示或在 / p>
until lately I could use bindProperty like shown below or in this question, but that has changed with 0.8.0 and I don't know how to change my code to get the old behaviour (doSomething() gets called):
<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());
logoutChangeSubscription = bindProperty(model, #isLoggedIn, () => doSomething());
}
}
class Model extends Object with ObservableMixin {
@observable bool isLoggedIn = false;
}
推荐答案
使用Polymer.dart 0.8或更高版本,您也可以使用这种方便的形式:
With Polymer.dart 0.8 or greater, you can also use this convenience form:
isLoggedInChanged(oldValue) {
doSomething();
}
注意如何在你的PolymerElement中创建一个方法, em> yourFieldName *已更改
Notice how you can create a method inside your PolymerElement that uses a name of yourFieldName*Changed
还有 onPropertyChange
,如下定义: http://api.dartlang.org/docs/bleeding_edge/observe.html#onPropertyChange
从文件:
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; }
}
这篇关于如何订阅可观察字段的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!