Where to store globals?

700 views
Skip to first unread message

Colin Cannon

unread,
Mar 5, 2016, 11:15:46 AM3/5/16
to Polymer
In my polymer app, where should I store global values used throughout my app?  For instance I have a lot of child elements that use iron ajax to call my api.  When I am developing I want to call my local api, but in production I want to call my production api.

<iron-ajax url="dev.example.com/api" ...></iron-ajax>

vs

<iron-ajax url="www.example.com/api" ...></iron-ajax>

Is there a cool polymer way to store a global config file with application constants?

Eric Bidelman

unread,
Mar 5, 2016, 1:14:11 PM3/5/16
to Colin Cannon, Polymer
You have a couple of options. 
1. This could be something the component could figure out internally at run time
2. you create a shared <app-state> element that is declared/used inside your component. See https://www.polymer-project.org/0.5/docs/polymer/polymer.html#global in the 0.5 docs.
3. you pass around globals using bindings. We've been doing this for the IO website this year, and it's working quite well. Think of it as dependency injection:

<my-app config="[[config]]"></my-app>

Then inside you'd use the config:

<iron-ajax url="[[config.url]]">

The only downside of this last approach is that it tends to get tedious. `config` needs to be passed down to every component (and sub component) that needs the state object.

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/a9faa04c-8b6e-4219-ac4f-5bea73a6d4b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Cannon

unread,
Mar 5, 2016, 11:55:06 PM3/5/16
to Polymer, colin...@gmail.com
Thanks Eric.  On the IO website where do you store global data specific to dev/prod?  For instance in my case my ajax calls will go to one url in dev and another in production.  Does your Polymer code store both sets of urls and other data and then Polymer checks the URL and then decides to use dev or prod data?

Right now I do use databinding so my app data is not spread out in all my components, but it is all still in my most parent component.  It would be nice to get it out of there.  I would go with option #2, but I think it would have to be a part of my code base (repo and webroot).

I am coming from a world where I did most of my dev with PHP and jQuery.  With PHP doing most of the work and rendering the pages I could keep an config file outside my webroot and outside my code repo and just have PHP include the file, which was mostly a bunch of defines.
Reply all
Reply to author
Forward
0 new messages