2016-11-11 41 views
0

我試圖填充基於thr的結果,我通過iron-ajax從Ajax請求中獲取數組 當我嘗試訪問我的屬性時,我的觀察窗口顯示「不可用」如何更改ajax請求中的聚合物v1.x屬性

下面是一個小例子我爲重新解釋我的問題:

我-component.html

<link rel="import" href="../../bower_components/polymer/polymer.html"> 
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html"> 
<link rel="import" href="../../bower_components/app-storage/app-localstorage/app-localstorage-document.html"> 
<link rel="import" href="../../bower_components/paper-dropdown-menu/paper-dropdown-menu.html"> 


<dom-module id="my-component"> 
    <template> 
     <div> 
      <paper-dropdown-menu 
        id="location" label="Ubicaci&oacute;n" 
        value="{{vehicle.location}}"> 
      </paper-dropdown-menu> 
      <paper-dropdown-menu id="city" label="Ciudad"> 
       <paper-menu 
         class="dropdown-content" 
         selected="{{city.city_code}}" 
         attr-for-selected="value" 
         on-iron-select="estadoSeleccionado"> 
        <template id="" is="dom-repeat" items="[[opcionesDisponibilidad]]" as="i"> 
         <paper-item value="[[i.valor]]">[[i.texto]]</paper-item> 
        </template> 
       </paper-menu> 
      </paper-dropdown-menu> 
     </div> 
     <iron-ajax 
       id="ajax" 
       method="GET" 
       url="http://localhost:8080/ws.asmx" 
       params="" 
       content-type="application/x-www-form-urlencoded; charset=UTF-8" 
       handle-as="text" 
       on-response="handlecities" 
       debounce-duration="300"> 
     </iron-ajax> 
     <app-localstorage-document id="localStorageElement" key="myapp.login_data" data="{{loginInfo}}"></app-localstorage-document> 
    </template> 
    <script> 
     Polymer({ 
      is: "my-component", 
      properties:{ 
       vehicle: { 
        type: Object, 
        value:{ 
         id: "", 
         plate: "", 
         location: "" 
        } 
       }, 
       cities:{ 
        notify: true, 
        type: Array, 
        value: [{city_code:'0', city_name:'-'}] 
       } 
      }, 
      ready:function() { 
       this.querycities(); 
      }, 
      querycities: function() { 
       if (this.$.localStorageElement.storage['myapp.login_data']){ 
        this.$.loginInfo = JSON.parse(this.$.localStorageElement.storage['myapp.login_data']); 
       } else return false; 
       this.$.ajax.url = this.$.ajax.url + "/listadoCiudades"; 
       this.$.ajax.verbose = true; 
       this.$.ajax._boundHandleResponse = this.handlecities; 
       this.$.ajax.params = {dispositivo:'12345', credencial: this.$.loginInfo.cred}; 
       this.$.ajax.generateRequest(); 
      }, 
      handlecities: function (request) { 
       console.log(request.response); 
       var xPath = new SoftXpath(); 
       xPath.registerNamespace("",""); 
       xPath.loadXML(request.response); 
       var cities = xPath.selectNodes("//Ciudad//*[self::Codigo|self::Nombre]"); 
       var xPathElement = new SoftXpath(); 
       if (cities){ 
        for(var i = 1; i < cities.length;i++){ 
         var c = new Object(); 
         c.Codigo = cities[i-1].text; 
         c.Nombre = cities[i].text; 

         this.$.cities.push(c);// <---- Here I have not access to my polymer property ' cities '. 

         //How to update my polymer var cities ?? 
         //Tried also with: 
         // this.cities --> undefined 
         // this.$$('#myassignedId').cities --> undefined 
        } 
       } 
      } 
     }) 
    </script> 
</dom-module> 

我怎麼能如果出現缺的填充我的「城市」屬性範圍 ?

+0

'handlecities()'需要設置'this.cities'到來自服務器的響應解析的結果。 'this.cities'不需要事先存在。沒有更多信息,您的手錶表達式問題無法調試。你用什麼表情?你在哪裏設置了一個斷點來查看錶達式?什麼是設置表達式的目標屬性的相關代碼? – tony19

回答

0

那麼, 經過太多的嘗試錯誤的變化,最後我找到了錯誤的原因。

65號線:

this.$.ajax._boundHandleResponse = this.handlecities; 

刪除了該行,我的所有屬性聚合物迴歸常態。

我在使用那句話,因爲我試圖重複使用這個組件來處理幾個ajax請求。 'handlecities'也被指定爲屬性,所以是多餘的(只有當沒有重用的意圖時)。我仍然不知道如何重新使用定義自定義處理程序的組件,但由於某種原因,如果我使用上述語句,則所有聚合物屬性都會丟失。

所以,在此期間I'm定義幾個AJAX組件一個用於每個Ajax請求