【问题标题】:Dart: Using observable with multiple getter/setter(s)Dart:使用带有多个 getter/setter 的 observable
【发布时间】:2017-12-08 16:47:02
【问题描述】:

对于我的 Polymer 应用程序,我需要一个具有两种不同风格的 observable,例如作为整数值和字符串值。我使用 getter 和 setter 来封装状态和内部表示。通过这样做,我必须为每个 setter 中的每个 observable 实现 notifyPropertyChange,这会导致很多容易出错的管道代码。例如 如果必须使用,我需要两次 notifyPropertyChange-Statements 两种风格 4 种口味,我必须使用 4*4 = 16 个 notifyPropertyChange-Statements。我已经修改了点击计数器示例来说明这一点:

@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++;
  }
}

如果没有那么多 notifyPropertyChange-Statements,有没有更好的方法来实现这一点?

问候

马库斯

【问题讨论】:

    标签: dart dart-polymer


    【解决方案1】:

    如果count 属性不需要是私有的——根据Dart style guide 需要考虑的事情——这种情况下会有一个不错的选择。

    class ClickCounter extends PolymerElement {
        @observable int count;
        ...
        void countChanged(oldValue) {
            // do something...
        }
    }
    

    对可观察属性的更改会自动传递给名为<property_name>Changed 的方法,其中property_name 指的是要监视的属性。

    希望这有助于为编写 Polymer 组件带来更多乐趣。

    【讨论】:

      【解决方案2】:

      不确定这是否适用于您,但我遇到了同样的问题。就我而言,虽然我可以只用一个属性和一个过滤器来解决它。

      飞镖类

      int count;
      
      int asInt(...) // can't remember details of filters
      

      html

      <input type="text">{{count | asInt}}<input>
      

      对代码不正确深表歉意。我在手机上,没有时间搜索代码。希望你能明白

      干杯 安德斯

      【讨论】:

        【解决方案3】:

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

        @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++;
          }
        }
        

        【讨论】:

        • 非常感谢。这行得通!所以我每个可观察符号只有一个 notifyPropertyChange。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 2020-03-17
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        相关资源
        最近更新 更多