聚合物 1 从深子 属性 中获取元素更改路径
Polymer 1 get element from deep sub-property changes path
我做了以下代码:
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 引用的对象的 'id'?
我的意思是我可以做一些事情来分析字符串并获取“0”,然后将其转换为整数,然后像这样获取对象 ID:
this.skills[varWith0].id
但这感觉不是正确的干净方式。我也觉得这是一件很常见的事情。
那么有什么干净的方法可以做到这一点吗?
谢谢
好的..我决定自己写代码来解决这个问题。就像我说的那样,我认为应该有一些标准的方法来做到这一点。尽管如此,这也有效。
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);
}
}
我知道评论是西班牙语的(对此感到抱歉),但对于不懂西班牙语的人来说,代码非常简单。希望对你有帮助
元素原型上有一个 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.push
和 this.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;
}
}
}
}
我做了以下代码:
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 引用的对象的 'id'?
我的意思是我可以做一些事情来分析字符串并获取“0”,然后将其转换为整数,然后像这样获取对象 ID:
this.skills[varWith0].id
但这感觉不是正确的干净方式。我也觉得这是一件很常见的事情。
那么有什么干净的方法可以做到这一点吗?
谢谢
好的..我决定自己写代码来解决这个问题。就像我说的那样,我认为应该有一些标准的方法来做到这一点。尽管如此,这也有效。
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);
}
}
我知道评论是西班牙语的(对此感到抱歉),但对于不懂西班牙语的人来说,代码非常简单。希望对你有帮助
元素原型上有一个 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.push
和 this.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;
}
}
}
}