将 iron-ajax 请求的返回值存储在变量中(Polymer)
Store returned values from the iron-ajax request in variables (Polymer)
我是第一次使用聚合物铁-ajax元素,遇到了一个我认为很容易解决的问题,但我无法在线找到解决方案。
我使用 iron-ajax 元素检索用户详细信息,并使用 dom-repeat 遍历它。用户的名字和姓氏被放置在我创建的用于显示用户的自定义卡片元素中。一切正常,除了我无法将返回的 id 值存储在变量中。
我确信这可以通过使用数据绑定来实现,但我无法让它工作。甚至当我尝试在 JavaScript 中使用 getAttribute 获取值时,它也只是 returns null.
在下面的代码中可以找到我的 iron-ajax 请求和响应,包括我笨拙地尝试获取 id 值 {{item.id}}。为此我创建了一个 on-当用户点击其中一张卡片时点击功能,然后应该获得 stdId 属性的值。
<dom-module id="my-students-view">
<template>
<style>
:host {
display: block;
}
</style>
<!-- Iron-Ajax created connection and gets data -->
<iron-ajax
auto
id="requestRepos"
url="http://api.dev/user/"
handle-as="json"
loading="{{isloading}}"
last-response="{{response}}"></iron-ajax>
<!-- dom-repeat iterates the response -->
<template is="dom-repeat" items="[[response.data]]" sort="sortData">
<student-card
id="studentCard"
to="http://localhost:8080/profile-view/{{item.id}}"
picsrc="../../images/anonymous.jpg"
name="{{item.first_name}} {{item.last_name}}"
stdId="{{item.id}}"
on-tap="sendId"></student-card>
</template>
</template>
<script>
Polymer({
is: 'my-students-view',
properties: {
stdId: {
notify: true,
}
},
//Sort the array/data alphabetically
sortData: function(a, b) {
if(a.first_name < b.first_name) return -1;
if(a.first_name > b.first_name) return 1;
return 0;
},
//Try to get the id value of a the student card
sendId: function() {
var idPropertie = this.$$("#studentCard");
var idValue = idPropertie.getAttribute('stdId');
console.log('the id is:' + idValue);
},
});
</script>
</dom-module>
重写sendId函数:
sendId: function(event) {
var idValue = event.model.item.id;
console.log('the id is:' + idValue);
},
我是第一次使用聚合物铁-ajax元素,遇到了一个我认为很容易解决的问题,但我无法在线找到解决方案。
我使用 iron-ajax 元素检索用户详细信息,并使用 dom-repeat 遍历它。用户的名字和姓氏被放置在我创建的用于显示用户的自定义卡片元素中。一切正常,除了我无法将返回的 id 值存储在变量中。
我确信这可以通过使用数据绑定来实现,但我无法让它工作。甚至当我尝试在 JavaScript 中使用 getAttribute 获取值时,它也只是 returns null.
在下面的代码中可以找到我的 iron-ajax 请求和响应,包括我笨拙地尝试获取 id 值 {{item.id}}。为此我创建了一个 on-当用户点击其中一张卡片时点击功能,然后应该获得 stdId 属性的值。
<dom-module id="my-students-view">
<template>
<style>
:host {
display: block;
}
</style>
<!-- Iron-Ajax created connection and gets data -->
<iron-ajax
auto
id="requestRepos"
url="http://api.dev/user/"
handle-as="json"
loading="{{isloading}}"
last-response="{{response}}"></iron-ajax>
<!-- dom-repeat iterates the response -->
<template is="dom-repeat" items="[[response.data]]" sort="sortData">
<student-card
id="studentCard"
to="http://localhost:8080/profile-view/{{item.id}}"
picsrc="../../images/anonymous.jpg"
name="{{item.first_name}} {{item.last_name}}"
stdId="{{item.id}}"
on-tap="sendId"></student-card>
</template>
</template>
<script>
Polymer({
is: 'my-students-view',
properties: {
stdId: {
notify: true,
}
},
//Sort the array/data alphabetically
sortData: function(a, b) {
if(a.first_name < b.first_name) return -1;
if(a.first_name > b.first_name) return 1;
return 0;
},
//Try to get the id value of a the student card
sendId: function() {
var idPropertie = this.$$("#studentCard");
var idValue = idPropertie.getAttribute('stdId');
console.log('the id is:' + idValue);
},
});
</script>
</dom-module>
重写sendId函数:
sendId: function(event) {
var idValue = event.model.item.id;
console.log('the id is:' + idValue);
},