簡體   English   中英

將 Iron-ajax 請求的返回值存儲在變量中(Polymer)

[英]Store returned values from the iron-ajax request in variables (Polymer)

我第一次使用 Polymers iron-ajax 元素,遇到了一個我認為很容易解決的問題,但我無法在網上找到解決方案。

我使用 iron-ajax 元素檢索用戶詳細信息,並使用 dom-repeat 循環遍歷它。 用戶的名字和姓氏被放置在我創建的用於顯示用戶的自定義卡片元素中。 一切正常,只是我無法將返回的 id 值存儲在變量中。 我確信使用數據綁定是可能的,但我無法讓它工作。 即使當我嘗試在我的 JavaScript 中使用 getAttribute 獲取值時,它也只會返回 null。

在下面的代碼中,可以找到我的 Iron-ajax 請求和響應,包括我嘗試獲取 id 值 {{item.id}} 的尷尬嘗試。為此,當用戶單擊其中一張卡片時,我創建了一個 on-tap 函數然后應該獲取 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);
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM