Polymer Dart, global variables and data bindings (observable)

前端 未结 2 1686
礼貌的吻别
礼貌的吻别 2020-12-20 01:59

I read Polymer API developer guide, but unfortunately it has examples only for JavaScript developers. Some examples I try to port into Dart, but in this case I get fail. Ple

2条回答
  •  情话喂你
    2020-12-20 02:55

    app_gobals.html

    
    
    
      
      
    
    

    app_gobals.dart

    import 'package:polymer/polymer.dart';
    import 'dart:async' show Timer;
    
    @CustomTag('app-globals')
    class AppGlobals extends PolymerElement {
      static final ObservableMap _staticValues = toObservable({});
    
      Map get values => _staticValues;
    
      AppGlobals.created() : super.created();
    
      ready() {
        attributes.keys.forEach((k) {
          values[k] = attributes[k];
        });
    
        // just to demonstrate that value changes are reflected
        // in the view
        new Timer.periodic(new Duration(seconds: 2),
            (_) => values['periodic'] = new DateTime.now());
      }
    }
    

    app_element.html (your my-component)

    
    
    
      
      
    
    

    app_element.dart

    import 'package:polymer/polymer.dart';

    @CustomTag('app-element')
    class AppElement extends PolymerElement {
      AppElement.created() : super.created();
    
      ready() {
        print('Last name: ${$["globals"].values["lastName"]}');
      }
    }
    

    @observable indicates that Polymer should be notified when the value changes so it can update the view. If you have a collection this is not enough because Polymer only gets notified when the field changes (another collection or null is assigned). toObservable(list|map) ensures that Polymer gets notified when elements in the collection are changed (removed/added/replaced). PolymerElement includes Observable there fore there is nothing special to do on class level. When you extend a DOM element this looks a bit different see https://stackoverflow.com/a/20383102/217408.

    Update
    This are a lot of questions. I use static final ObservableMap _staticValues = toObservable({}); to ensure all values are stored in one place no matter how many elements your application contains. Statics are stored in the class not in the instance therefore it doesn't matter how many instances exist. @ComputedProperty(expression) var someField; watches expression for value changes and notifies Polymer to update bindings to someField. @observable is the simpler version but works only for fields. @published is like @observable but in addition allows bindings to the field from outside the element. @PublishedProperty() is the same as @published but this annotation form allows to pass arguments. @PublishedProperty(reflect: true) is like @published but in addition updates the actual DOM attribute to make the bound value available not only for other Polymer elements to bind to but also for CSS or other Frameworks which have no knowledge how to bind to Polymer fields.

提交回复
热议问题