如何在Dart / Polymer Dart中创建可观察的导数属性? [英] How do you create an observable derivative property in Dart / Polymer Dart?
问题描述
我有一个组件,我想绑定一个不同的CSS类基于一个布尔值。我在我的组件代码中有以下:
bindCssClass(div,open,this,task.isOpen);
bindCssClass(div,closed,this,'task.isClosed');
isOpen / isClosed的定义如下:
@observable bool isOpen = true;
get isClosed => !开了;问题是,如何获得isClosed是可观察的,但是基于对isOpen的更改吗?我想知道这种情况,但也适用于更复杂的情况(例如,从多个组件派生的字符串)
此外,有更好的方法使用bindCss这样一个简单的case?绑定到'!task.isOpen'不工作,虽然它会很好,如果它是。
解决方案以检查来自dart-polymer-dart-examples github repo的observable_getter示例。
with ObservableMixin {
@observable
DateTime timestamp;
App(){
bindProperty(this,const Symbol('timestamp'),
()=> notifyProperty(this,const Symbol('second')) ;
}
int get second => timestamp.second;
}
main(){
App app = new App();
new Timer.periodic(const Duration(seconds:1),(_){
app.timestamp = new DateTime.now();
});
query('#tmpl')。model = app;
}
同时检查 https://code.google.com/p/dart/issues/detail?id=12473
I have a component that I want to bind a different css class to based on a boolean value. I have the following in my component code:
bindCssClass(div, "open", this, "task.isOpen");
bindCssClass(div, "closed", this, 'task.isClosed');
Where isOpen / isClosed are defined as the following:
@observable bool isOpen = true;
get isClosed => !isOpen;
The question is, how can I get isClosed to be observable, but based on changes to isOpen? I'd like to know this case, but also for cases that are more complicated (e.g. a string that is derived from multiple components)
Additionally, is there a better way to use bindCss for such a simple case? Binding to '!task.isOpen' doesn't work, though it would be nice if it did.
解决方案 you may want to check observable_getter example from dart-polymer-dart-examples github repo.
class App extends Object with ObservableMixin {
@observable
DateTime timestamp;
App() {
bindProperty(this, const Symbol('timestamp'),
() => notifyProperty(this, const Symbol('second')));
}
int get second => timestamp.second;
}
main() {
App app = new App();
new Timer.periodic(const Duration(seconds: 1), (_) {
app.timestamp = new DateTime.now();
});
query('#tmpl').model = app;
}
Also check discussion at: https://code.google.com/p/dart/issues/detail?id=12473
这篇关于如何在Dart / Polymer Dart中创建可观察的导数属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!