In Polymer 0.5 the advice on globals was as outlined in this question/answer:
However in Polymer 1.0 this doesn't seem to work. Change notifications are not automatically generated on the underlying model, they are generated on the <dom-module>
instead which means that change notifications will be generated on only one of the <app-globals>
.
What is the recommended way of implementing this pattern in Polymer 1.0?
Polymer element
<iron-meta>
is also an option. For me this was the easiest solution.Sjmiles, one of Polymer's creators just posted the following snippet to the Polymer slack room as an example of shared data:
I've actually moved my app to using simple data binding, so I'm not sure of the validity of this approach, but maybe it would be useful to someone.
I've extended Etherealones' solution to work as a Behavior, and to extend Polymers "set" and "notifyPath" methods to trigger the updates automatically. This is as close as i could get to a true databinding across components/elements:
globals-behavior.html:
And in all polymer elements that should have access to the globals variable:
Examples:
It is much easier to achieve the same effect of global variables if you wrapped your application in a template. Watch the explanation in this video (I linked to the exact minute and second where the concept is explained).
I've combined all suggestions above into the following global polymer object
and use it elsewere like
Changing either data1.loader or data2.loader affects other instances. You should to extend commondata object to add more global properties like it shown with loader property.
I have implemented a pattern like
iron-signals
uses for this purpose. So the basic principle is that you manually notify other instances when an update occurs.Consider this:
You will simple call the version that have an underscore suffix like
fire_
when you are firing an event. You can even create a Polymer Behavior of some sort with this pattern I guess.Beware that preceding underscore properties are already used by Polymer so don't go ahead and convert these to
_fire
.P.S.: I didn't look around to solve how to reflect the notification of
this.push(array, value);
as I don't need it. I don't know if it's possible this way. Should go findPolymer.Base.push
.