dman
dman

Reputation: 11064

Polymer - best way to implement a global value for data binding?

What is the best way to implement a GOBAL_URL value in a polymer 1.0 for all elements? Like in Angular 1 you have a constant service.

  <form is="iron-form"
    id="search"
    method="get"
    with-credentials="true"
    action="{{GOBAL_URL}}/api/v1/food_search"
    content-type="application/x-www-form-urlencoded; charset=UTF-8">

Upvotes: 0

Views: 211

Answers (2)

tony19
tony19

Reputation: 138266

For a server URL, I use localStorage (or the <app-localstorage-document> element). It would look something like:

<app-localstorage-document key="serverUrl" data="{{_serverUrl}}"></app-localstorage-document>
<iron-ajax url="[[_serverUrl]]/api/v1/food_search"></iron-ajax>

The _serverUrl property is initialized to a default URL.

Upvotes: 1

Kasia
Kasia

Reputation: 1725

I would go with using iron-meta element to share env variables ->link

You can then use iron-meta-query to get global variable. You can even store object in there.

<iron-meta id="fanPages" key="fanPages" value=
        '{
          "fb": "https://facebook.com/your-site",
          "tw": "https://twitter.com/your-site",
          "inst":"https://www.instagram.com/your-site"
        }'>

Then in any other component you can get this variable by query it

<iron-meta-query id="query" key="fanPages" value="{{fanPages}}"></iron-meta-query>

Upvotes: 2

Related Questions