How to share data structures with Polymer on both client and server

后端 未结 1 1486
余生分开走
余生分开走 2021-01-23 03:10

Problem: I have a dart file defining some data structures, which I need to use both for the client and for the server. I\'d like to make these data structures o

1条回答
  •  南方客
    南方客 (楼主)
    2021-01-23 03:39

    You can use the observe package.

    With ChangeNotifier you initiate the change notification yourself by calling notifyPropertyChange when a value changes. The changes get delivered synchronously.

    Observable needs dirtyCheck() to be called to deliver changes.

    Polymer calls Observable.dirtyCheck() repeatedly to get the changes automatically.

    an example for each

    import 'package:observe/observe.dart';
    
    class Notifiable extends Object with ChangeNotifier {
      String _input = '';
    
      @reflectable
      get input => _input;
    
      @reflectable
      set input(val) {
        _input = notifyPropertyChange(#input, _input, val + " new");
      }
    
      Notifiable() {
        this.changes.listen((List record) => record.forEach(print));
      }
    }
    
    class MyObservable extends Observable {
      @observable
      String counter = '';
    
      MyObservable() {
        this.changes.listen((List record) => record.forEach(print));
      }
    }
    
    void main() {
      var x = new MyObservable();
      x.counter = "hallo";
      Observable.dirtyCheck();
    
      Notifiable notifiable = new Notifiable();
      notifiable.input = 'xxx';
      notifiable.input = 'yyy';
    }
    

    0 讨论(0)
提交回复
热议问题