Reputation: 352
How to data bind in the new Polymer v1.0?
I saw an answer in polymer iron-ajax : How to Bind data from input element to iron-ajax's body attribute
But it did not help me and Here is my code
<dom-element id="test-app>
<template>
...
<iron-ajax
auto
url="https://www.googleapis.com/youtube/v3/search"
params="{{ajaxParams}}"
handleAs="json" lastResponse="{{response}}"
method='GET'>
</iron-ajax>
</template>
</dom-module>
Script
Polymer({
is:"Test-app",
properties: {
qry: {
type: String,
value: 'Cat'
},
key1: {
type: String,
value: 'myapikey'
},
part1: {
type: String,
value: 'snippet'
},
maxResults1: {
type: Number,
value: 10
},
ajaxParams: {
type: String,
computed: 'processParams(part1, qry, maxResults1, key1)'
}
},
processParams: function(part1, qry, maxResults1, key1){
var param = JSON.stringify({part: part1, q:qry, maxResults: maxResults1, key:key1});
console.log(param);
return param;
}
});
</script>
I get the correct log in console as a JSON string, but when this value is being returned, the value is taken literally (same problem as told in the link provided above) and not as its value.
I get an error in console as bad request code 400. Any help is highly appreciated.
Upvotes: 4
Views: 6998
Reputation: 117
<template is="dom-bind" id="app">
<iron-ajax auto
url="https://www.googleapis.com/youtube/v3/search"
params='{"part":"snippet", "q":"polymer", "key": "AIzaSyAuecFZ9xJXbGDkQYWBmYrtzOGJD-iDIgI", "type": "video"}'
handle-as="json"
last-response="{{ajaxResponse}}"></iron-ajax>
<template is="dom-repeat" items="[[ajaxResponse.items]]">
<div class="horizontal-section">
<h2><a href="[[url(item.id.videoId)]]" target="_blank">[[item.snippet.title]]</a></h2>
<iron-image src="[[item.snippet.thumbnails.high.url]]" width="256" height="256" sizing="cover" preload fade></iron-image>
<p>[[item.snippet.description]]</p>
</div>
</template>
<script>
var app = document.querySelector('#app');
app.url = function (videoId) {
return 'https://www.youtube.com/watch?v=' + videoId;
};
</script>
I'm sorry. the ajax query returns a json array of Youtube videos with the paramaters listed in the property "params". the one way binding "[[ajaxResponse.items]]" binds the response to the repeating template "dom-repeat". the url function just appends the videoId to the individual links. I copied the code straight from the iron-ajax demo that ships with polymer starter kit
Upvotes: -2
Reputation: 2806
The property params
is of type Object
. Unlike in the linked example, you can just return a native object.
processParams: function(part1, qry, maxResults1, key1) {
return {
part: part1,
q: qry,
maxResults: maxResults1,
key:key1
};
}
Upvotes: 2