2016-03-21 18 views
1

從以下文檔https://www.polymer-project.org/1.0/docs/devguide/properties.html#deep-sub-property-changes-on-array-items聚合物1個GET元件,從而改變路徑

我做了如下代碼:

  properties:{ 
       skills:{ 
        type:Array, 
        notify: true, 
        value: [ 
         { 
          id:1, 
          name:"UNOOOOOO", 
          description: "aaa a a aa a aaaa adicionales con el fin de exceder sus expectativas y "+ 
          "mejorar su calidad de vida. La orientación al "+ 
          "cliente es una actitud permanente que caracteriza a la persona." 
         }, 
         { 
          id:2, 
          name:"Capacidad para plantear identificar y resolver problemas", 
          description: "aaa a a aa a aaaa adicionales con el fin de exceder sus expectativas y "+ 
          "mejorar su calidad de vida. La orientación al "+ 
          "cliente es una actitud permanente que caracteriza a la persona." 
         } 
        ] 
       } 
      }, 
      observers: [ 
       'skillIsSelectedChanged(skills.*)' 
      ], 
      skillIsSelectedChanged: function(changeRecord){ 
       console.log(changeRecord); 
      } 

我能夠通過數據綁定更新以獲得回調。 :d

Object {path: "skills.#0.isSelected", value: true, base: Array[2]} 

我的問題是:有沒有獲得一個由#0引用的對象的「身份證」的任何干淨的方式?

我的意思是我可以做一些分析字符串並獲得「0」,然後將其轉換爲整數,然後獲取對象ID是這樣的:

this.skills[varWith0].id 

但這並不覺得像正確的清潔方式。我也覺得這是一件很平常的事情。

那麼有沒有乾淨的方式做到這一點?

謝謝

+0

使用調試器..通瀏覽一長串Ø f對象上的「obj更新」。看看你是否可以在某處找到ID的引用 –

+0

感謝您的回覆!我不確定我明白。什麼si「obj更新」?我在哪裏以及如何找到它?謝謝 – Alejandro

回答

0

好吧。我決定寫我自己的代碼來解決這個問題。就像我說的那樣,我認爲應該有一些方法來做到這一點。儘管如此,這也起作用。

  skillIsSelectedChanged: function(changeRecord){ 
       //primero tenemos que obtener la posicion del skill que cambio. 
       //path nos da un string asi: skills.#0.isSelected donde el 0 despues del # es la posicion. 
       //por otro lado cuando se cargan los elementos inicialmente tambien llama este callback 
       //pero con un string en path asi: skills 

       //primero hacemos split en #. 
       var arr = changeRecord.path.split("#"); 

       //luego si el length del arreglo es mayor a 1 estamos en el caso que nos interesa. 
       if(arr.length > 1){ 
        //como desconocemos el largo del numero, le volvemos e hacer un split al arr[1] en '.' 
        //de ese segundo arreglo resultante, la posicion 0 es el numero deseado. Ese lo convertimos en int 
        var arr2 = arr[1].split("."); 
        var position = parseInt(arr2); 

        //finalmente buscamos en skills el id del objeto en la posicion obtenida 
        var id = this.skills[position].id; 

        //lo metemos en el arreglo de respuesta 
        this.responseSkills.push(id); 
       } 
      } 

我知道評論是在西班牙(遺憾的事情),但人們不理解西班牙語的代碼非常簡單。希望它有幫助

1

在元素的原型上有一個get方法可用於通過路徑檢索值。所以如果你有路徑,你可以檢索這個值。

if (changeRecord.path.indexOf('.isSelected') >= 0) { 
    var itemPath = changeRecord.path.replace('.isSelected', ''); 
    var item = this.get(itemPath); 
} 

這也是方便地知道,你可以通過路徑的路徑或路徑段數組,所以你也可以這樣做:

var parts = changeRecord.path.split('.'); 
var last = parts.pop(); 
if (/* last is something interesting */) { 
    var item = this.get(parts); 
} 

如果你想確保數據綁定和觀察員更新後,你還需要使用this.pushthis.splice更新您的responseSkills數組,所以最終代碼可能看起來是這樣的:

if (changeRecord.path.indexOf('.isSelected') >= 0) { 
    var itemPath = changeRecord.path.replace('.isSelected', '.id'); 
    var id = this.get(itemPath); 
    if (changeRecord.value) { 
    this.push('responseSkills', id); 
    } else { 
    for (var i=0; i<this.responseSkills.length; i++) { 
     if (this.responseSkills[i] == id) { 
     this.splice('responseSkills', i, 1); 
     break; 
     } 
    } 
    } 
}