PolymerElements / iron-ajax

Easily make ajax requests
https://www.webcomponents.org/element/PolymerElements/iron-ajax
127 stars 113 forks source link

Auto requests with interpolated binded-props in the URL should wait for all bindings to be set #241

Open nicholaswmin opened 8 years ago

nicholaswmin commented 8 years ago

Description

Setting the url with multiple, interpolated binded-values triggers a request even if not all bindings have been defined when auto is set to true

Expected outcome

http://foo.com/users/ instead of http://foo.com/users/3

Steps to reproduce

Described above

Notes

Now to be clear -This can be solved in many ways - setting up an observer of some sort, a method to check the url value, or maybe a computed property that waits until all properties are defined.

For example I could do this:

<iron-ajax method="GET" url$="{{_processUrl(baseUrl, endpoint)}}" auto></iron-ajax>

<script>
...
  _processUrl: (baseUrl, endpoint) => {
    // @HACK checking if the `url` has all interpolated props set properly.
    // If it contains `//` or ends with `/` we infer that some of the binded props
    // have not been defined yet.
    if (endpoint.includes("//") || (endpoint.endsWith("/"))) return false;
    return baseUrl + "/" + endpoint;
  }
</script>

and usage:

 <foo-x endpoint="user/[[idUser]]"></foo-x>

However, I'd much prefer if I didn't have to write boilerplate code for trivial use cases like this one. If auto is really gonna be useful it should work for basic real-world cases like this one

Browsers Affected