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. Please, go to https://www.polymer-project.org/0.5/docs/polymer/polymer.html#global (Section Supporting global variables). Here is a clip from the documentation:
elements.html
<polymer-element name="app-globals">
<script>
(function() {
var values = {};
Polymer({
ready: function() {
this.values = values;
for (var i = 0; i < this.attributes.length; ++i) {
var attr = this.attributes[i];
values[attr.nodeName] = attr.value;
}
}
});
})();
</script>
</polymer-element>
<polymer-element name="my-component">
<template>
<app-globals id="globals"></app-globals>
<div id="firstname">{{$.globals.values.firstName}}</div>
<div id="lastname">{{$.globals.values.lastName}}</div>
</template>
<script>
Polymer({
ready: function() {
console.log('Last name: ' + this.$.globals.values.lastName);
}
});
</script>
</polymer-element>
index.html
<app-globals id="globals" firstname="Addy" lastname="Osmani"></app-globals>
Questions:
- How to implement this code in Dart?
- Reading the code of different Q&A concerning Dart Polymer usage I come across with
@observable
annotation,toObserve()
function andclass CustomElement extends PolymerElement with Observable {..}
. I know that they somehow related with data bindings, but I have no idea exactly how.
I found another solution that works for me:
Just define a class for your globals. Use a factory constructor so that every representation of the class is the same instance. The class must extend Object with Observable:
globals.dart:
Now you can use this class inside of all your custom elements. Only need to import globals.dart and create an object of it:
inside: main_app.dart:
and inside of main_app.html:
This works for me and it seems to be the easiest way to have something like globals with polymer custom elements.
app_gobals.html
app_gobals.dart
app_element.html (your my-component)
app_element.dart
import 'package:polymer/polymer.dart';
@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 ornull
is assigned).toObservable(list|map)
ensures that Polymer gets notified when elements in the collection are changed (removed/added/replaced).PolymerElement
includesObservable
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<app-globals>
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;
watchesexpression
for value changes and notifies Polymer to update bindings tosomeField
.@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.