Skip to content

Instantly share code, notes, and snippets.

@mmfilesi
Created March 13, 2016 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmfilesi/d0e96542db1872e3d17b to your computer and use it in GitHub Desktop.
Save mmfilesi/d0e96542db1872e3d17b to your computer and use it in GitHub Desktop.
demo helpers polymer (if and repeat)
<dom-module id="foo-component">
<template>
<h3>Frutas</h3>
<template id="templateFruits" is="dom-repeat" filter="filterFruits" items="{{fruits}}" sort="orderFruits">
<template is="dom-if" if="{{item.color}}">
<p>La fruta {{index}} es {{item.name}}
y es de color {{item.color}} <br>
<button on-tap="setFavorite">favorita</button></p>
</template>
</template>
Mi favorita es: {{fruitFavorite}}
<p><button on-tap="addFruit">añadir</button> </p>
</template>
<script>
Polymer({
is: 'foo-component',
properties: {
fruits: {
type: Array,
value: [
{'name': 'cerezas', 'color': 'rojo'},
{'name': 'mandarinas', 'color': 'naranja'},
{'name': 'fresas', 'color': 'rojo'},
{'name': 'peras', 'color': 'verde'},
{'name': 'kiwis', 'color': ''}
]
/* value: [] */
},
fruitFavorite: {
type: String,
value: "ninguna"
}
},
filterFruits: function(item) {
if ( item.color == 'rojo' || item.color == 'amarillo' ) {
return true;
}
return false;
},
orderFruits: function(a, b) {
return a.name < b.name ? -1 : 1;
},
setFavorite: function(e) {
var fruit = e.model.item;
this.fruitFavorite = fruit.name;
/* use console.log to understand what is polymer model ; ) */
console.log('e', e.model);
},
addFruit: function() {
this.push('fruits', {'name': 'piña', 'color': 'amarillo'});
}
});
</script>
</dom-module>
@mmfilesi
Copy link
Author

Una explicación detallada en the bit jazz band

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment