[英]Data binding in Polymer
I have this function which gives me a JSON return value when it is called. 我有这个函数,它在调用时给我一个JSON返回值。
getJSON function(url, success){
var ud = '_' + +new Date,
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
success && success(data);
};
script.src = url.replace('callback=?', 'callback=' + ud);
head.appendChild(script);
}
And to call the function, I use the following code. 并使用以下代码来调用该函数。
getJSON('https://newsapi.org/v1/articles?source=techcrunch&apiKey={APIKEY}', function(data){
//Stuff to be done with the data
});
And then I have a paper card to which I want to bind the JSON value that I get. 然后我有一张纸卡,要将绑定的JSON值绑定到该纸卡。
<paper-card heading="Card Title">
<div class="card-content">{{json}}</div>
</paper-card>
What I want to do is that call that declare the getJSON function the polymer way, call the function and set the JSON value returned to the {{json}}
data element in the paper card. 我要做的是以聚合方式声明getJSON函数的调用,调用该函数并设置返回纸卡中{{json}}
数据元素的JSON值。 I have tried more than 5 ways of doing the above but I could not do what I wanted to. 我已经尝试了5种以上的方法来完成上述任务,但我做不到自己想做的事情。 I am new to polymer so please help me. 我刚接触聚合物,所以请帮助我。
Instead of writing your own getJSON()
method, you could use Polymer's <iron-ajax>
element to fetch the data for you. 除了编写自己的getJSON()
方法,您还可以使用Polymer的<iron-ajax>
元素为您获取数据。
The News API data looks similar to this JSON object: News API 数据看起来类似于以下JSON对象:
{
"status": "ok",
"source": "the-next-web",
"sortBy": "latest",
"articles": [{
"author": "TNW Deals",
"title": "4 offers from TNW Deals you won’t want to miss",
"description": "We’ve featured some great offers from TNW …",
}, {
"author": "Bryan Clark",
"title": "Amazing 4k video of the Northern Lights",
"description": "Tune in, and zone out …",
}]
}
And I assume you want to display each article from the articles[]
array. 我假设您想显示articles[]
数组中的每篇文章。
The <iron-ajax>
element could request the data from the News API and store the server response in lastResponse
, which you could bind to a property that could be used in your template. <iron-ajax>
元素可以从News API请求数据,并将服务器响应存储在lastResponse
,您可以将其绑定到可以在模板中使用的属性。
In the following example, where we see last-response="{{data}}"
, <iron-ajax>
will output the News API response into data
(ie, like setting this.data = response
, where response
is the JSON object above). 在以下示例中,我们看到last-response="{{data}}"
, <iron-ajax>
将把News API响应输出到data
(即,像设置this.data = response
,其中response
是JSON对象以上)。 Given the shape of the data mentioned previously, we know that data.articles
would access the array of articles, which could be passed to dom-repeat
for iteration: 考虑到前面提到的数据的形状,我们知道data.articles
将访问文章数组,可以将其传递给dom-repeat
进行迭代:
<template>
<iron-ajax url="https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=|APIKEY|"
auto
last-response="{{data}}">
</iron-ajax>
<template is="dom-repeat" items="[[data.articles]]">
<paper-card heading="[[item.title]]">
<div class="card-content">
<p>[[item.description]]</p>
</div>
</paper-card>
</template>
</template>
Alternatively if you need to manipulate the response imperatively beforehand, you could setup an event listener for the <iron-ajax>.response
event. 另外,如果您需要事先强制性地处理响应,则可以为<iron-ajax>.response
事件设置事件侦听器。 The event detail contains the data in .response
. 事件详细信息包含.response
中的数据。 You could process/modify that data, and assign results to this.articles
, which is bound in the dom-repeat
. 您可以处理/修改该数据,并将结果分配给dom-repeat
绑定的this.articles
。
<template>
<iron-ajax url="https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=|APIKEY|"
auto
on-response="_onResponse">
</iron-ajax>
<template is="dom-repeat" items="[[articles]]">
<paper-card heading="[[item.title]]">
<div class="card-content">
<p>[[item.description]]</p>
</div>
</paper-card>
</template>
</template>
<script>
Polymer({
...
_onResponse: function(e) {
var data = e.detail.response;
// do something with data...
// set this.articles for dom-repeat
this.articles = data;
}
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.