2

In my Polymer app, I want to read a JSON file, for this I use an element. Part of the url is send by the parent element of the element currently using this . My String is properly recovered, i tried to just display it and it return exactly what I want. The problem is that if I just put the String in my url path like this :

<iron-ajax auto url="questions/{{path}}.json" handle-as="json" last-response="{{questions}}"></iron-ajax>

It doesn't work, I read on other threads that the cause of this is the use of a dynamic String which can't be used in the url path, as the String is data-binded.

If I wrote the url manually it works just fine :

<iron-ajax auto url="questions/listQuestions.json" handle-as="json" last-response="{{questions}}"></iron-ajax>

So I tried to compute my value to just return a String but it doesn't work either. It's has been hours of trying to come up with a solutions and research one on Internet but it just doesn't work.

Here's my code with the computed properties I tried :

properties: {
            path :String,

            url: {
                type: String,
                notify: true,
                computed: 'computeurl(path)'
            }
},

_acces: function(path) {
            return "questions/"+path+".json";
},

computeurl: function(path) {
            return path;
}

When I tried to display them like this :

<p><span>[[_acces(path)]] or [[url]] or [[path]]</span></p>

I got :

Display computed properties

Rhohen
  • 35
  • 5

2 Answers2

1

If you want to use a bind to some property like this you need to use it with $ symbol. Your example must have the view like this:

<iron-ajax auto url$="questions/{{path}}.json" handle-as="json" last-response="{{questions}}"></iron-ajax>
Dmytro
  • 345
  • 7
  • 14
  • I already tried it and it still doesn't work, I also tried to generate my request on my ready function like this : `this.$.ajaxLoader.generateRequest()` – Rhohen Apr 18 '17 at 07:30
  • The `$` is only necessary if you're trying to bind to an attribute on an element in the DOM. – codeMonkey Apr 28 '17 at 22:45
0

This ought to do you:

<template>
    <iron-ajax auto
               url="[[_computeUrl(path)]]"></iron-ajax>
</template>
<script>
    ...
    properties: {
        "path": {
            type: String,
            value: function() {
                return 'listQuestions';
            }
        }
    },
    _computeUrl: function(path) {
        return 'questions/' + path + '.json';
    }

</script>

Update 2017-4-26: After reviewing your paste, you were missing the bower_component reference to iron-ajax; see pastebin here: https://pastebin.com/ShqzedyW. Also listed a couple of your properties that you hadn't named, made url a computed property. Check it out now.

codeMonkey
  • 4,134
  • 2
  • 31
  • 50