Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer iron-ajax element params with databinding is splitting the parameter into single chars

I have a problem with Polymers iron-ajax element. When calling it like this:

 <iron-ajax url="https://api.onedrive.com/v1.0/drive/root" params='{"access_token":"[[access_token]]"}'></iron-ajax>

It sends a url like this, splitting the whole params string into multiple parameters:

https://api.onedrive.com/v1.0/drive/root?0="&1=a&2=c&3=c&4=e&5=s&6=s&7=_&8=t&9=o&10=k&11=e&12=n&13="...

When using a normal String as parameter it works correctly so i guess the quotes are correct.

The script part of the Element which uses iron-ajax:

<script>

  Polymer({
  is: 'onedrive-files',
  properties: {
    access_token: String
  },

  ready: function() {
  },
});
</script>

and i am calling the element like this:

<onedrive-files access_token="testtoken">
</onedrive-files>

Does anyone have any ideas? Thanks!

Edit: With an getter Function:

    <dom-module id="onedrive-files">
      <template>
    <iron-ajax id="ajax" url="https://api.onedrive.com/v1.0/drive/root" last-response="{{data}}" params='{{_getParams()}}' auto></iron-ajax>
  </template>
   <script>
     Polymer({
      is: 'onedrive-files',
      properties: {
        access_token: String
      },

      _getParams: function()
      {
        return ('{"access_token":"' + this.access_token + '"}');
      },

      ready: function() {
        this.$.ajax.generateRequest();    
    },
    });

    </script> 
    </dom-module>

With setting the Param in the Ready function:

<dom-module id="onedrive-files">
  <template>
    <iron-ajax id="ajax" url="https://api.onedrive.com/v1.0/drive/root" last-response="{{data}}" auto></iron-ajax>
  </template>

  <script>

    Polymer({
      is: 'onedrive-files',
      properties: {
        access_token: String
      },

      ready: function() {
        this.$.ajax.params = '{"access_token":"' + this.access_token + '"}';
    },
    });

  </script>
</dom-module>
like image 275
Sosian Avatar asked Dec 08 '15 19:12

Sosian


2 Answers

It seems like this is another limitation of dynamic attributes. So, the usual fallback for such cases are getter functions :

 <iron-ajax url="https://api.onedrive.com/v1.0/drive/root" params='{{_getParams(access_token)}}'></iron-ajax>

...

<script>

  Polymer({
  is: 'onedrive-files',
  properties: {
    access_token: String
  },
  _getParams:function(access_token) {
       return {access_token:access_token};
  }
});
</script>
like image 134
Christoph Sonntag Avatar answered Oct 07 '22 14:10

Christoph Sonntag


The params property is an Object, and it looks like the parsing from the html attribute (which is String) only happens on attached.

IMHO the easiest solution is to set it before you generate the request. But set an Object, not an String. It will generate one GET param foreach element of the params Object, a String is a list of characters, so one GET param for each character...

this.$.myAjaxElement.set( 'params', {"access_token": this.access_token });
this.$.myAjaxElement.generateRequest();
like image 7
Chu Avatar answered Oct 07 '22 15:10

Chu