Dart:使用带有多个getter / setter的observale [英] Dart: Using observale with multiple getters/setter

查看:196
本文介绍了Dart:使用带有多个getter / setter的observale的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Polymer应用程序我需要一个observable在两种不同的口味,例如作为一个整数值和字符串值。我使用getters和setters来封装状态和内部表示。通过这样做,我必须实现 notifyPropertyChange 每个观察者在每个setter,这导致了大量的errorphrone管道代码。例如
我需要两次 notifyPropertyChange - 两种口味的语句,如果我必须使用
4种口味,我必须使用4 * 4 = 16 notifyPropertyChange - 语句。我修改了点击计数器示例来说明这一点:

  @CustomTag('click-counter')
class ClickCounter extends PolymerElement {
int _count;
@observable int get count => _计数;
@observable set count(int val){
notifyPropertyChange(#count,_count,val);
_count = notifyPropertyChange(#strcount,_count,val);}

@observable String get strcount {
print(TOSTRING+ _count.toString());
return _count.toString();}

@observable set strcount(String val){
notifyPropertyChange(#strcount,_count,int.parse(val));
_count = notifyPropertyChange(#count,_count,int.parse(val));}

ClickCounter.created():super.created(){
}

void increment(){
count ++;
}
}

有没有更好的方法来实现这么多 notifyPropertyChange - 语句?



尊敬



Markus

解决方案

我没有测试它,但它应该工作,我认为它显着更少的代码,如果你有更多的这样的属性。



类别ClickCounter extends PolymerElement {$(




















$ b b $ b int _count;
@observable int get count => _计数;
set count(int val){
notifyPropertyChange(#count,_count,val);
notifyPropertyChange(#strcount,_count.toString(),val.toString());
_count = val;

@observable String get strcount {
// print(TOSTRING+ _count.toString());
return _count.toString();}

set strcount(String val){
count = int.parse(val); //设置新的值使用setter不是字段来触发属性更改
}

ClickCounter.created():super.created();

void increment(){
count ++;
}
}


for my Polymer-application I need one observable in two different flavors, for example as an integer-value and and string-value. I use getters and setters to encapsulate the state and internal representation. By doing this I have to implement notifyPropertyChange for every observable in every setter, which leads to much errorphrone plumbing code. For example I need two times to notifyPropertyChange-Statements for two flavors, if I have to use 4 flavors, I have to use 4*4 = 16 notifyPropertyChange-Statements. I have modified the click-counter example to illustrate this:

@CustomTag('click-counter')
class ClickCounter extends PolymerElement {
  int _count;
  @observable int get count => _count;
  @observable set count(int val) {
    notifyPropertyChange(#count,_count,val);
    _count = notifyPropertyChange(#strcount,_count,val);}

  @observable String get strcount { 
    print("TOSTRING "+_count.toString()); 
    return _count.toString();}

  @observable set strcount(String val) { 
    notifyPropertyChange(#strcount,_count,int.parse(val));
    _count = notifyPropertyChange(#count,_count,int.parse(val));}

  ClickCounter.created() : super.created() {
  }

  void increment() {
    count++;
  }
}

Is there a better way to implement this without so much notifyPropertyChange-Statements?

Regards

Markus

解决方案

I have not tested it, but it should work and I think its notable less code if you have more such properties.

@CustomTag('click-counter')
class ClickCounter extends PolymerElement {
  int _count;
  @observable int get count => _count;
  set count(int val) { 
    notifyPropertyChange(#count,_count,val);
    notifyPropertyChange(#strcount, _count.toString(), val.toString());
    _count = val;

  @observable String get strcount { 
    // print("TOSTRING "+_count.toString()); 
    return _count.toString();}

  set strcount(String val) { 
    count = int.parse(val); // set the new value using the setter not the field to fire property change
  }

  ClickCounter.created() : super.created();

  void increment() {
    count++;
  }
}

这篇关于Dart:使用带有多个getter / setter的observale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆