Skip to content

Instantly share code, notes, and snippets.

@esbanarango
Forked from sduquej/controllers.application.js
Last active February 12, 2020 04:20
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 esbanarango/8d6000d66d157e22e110be4c9ffb8545 to your computer and use it in GitHub Desktop.
Save esbanarango/8d6000d66d157e22e110be4c9ffb8545 to your computer and use it in GitHub Desktop.
Editing array of strings
import Ember from 'ember';
import { set } from '@ember/object';
export default Ember.Controller.extend({
primitiveFruits: ['banana' ,'apple'],
wrappedFruits: [{name: 'banana'}, {name: 'apple'}],
printPrimitiveFruits() {
console.log(this.get('primitiveFruits'));
},
printWrappedFruits() {
console.log(this.get('wrappedFruits').mapBy('name'));
},
updateValue(index, {target: {value} }){
this.primitiveFruits[index] = value;
set(this, 'primitiveFruits', [...this.primitiveFruits]);
}
});
<h1>Change some fruits</h1>
<h2>Using the text inputs I want to edit the contents of the fruits array</h2>
<h3>Using primitives 👎</h3>
{{#each primitiveFruits key="@index" as |fruit index|}}
{{fruit}}
<input type="text" value={{fruit}} onkeyup={{action this.updateValue index}}>
{{/each}}
<button {{action printPrimitiveFruits}}>Print</button>
<br />
<h3>As properties of an object 👍</h3>
{{#each wrappedFruits as |fruit|}}
{{fruit.name}}
{{input value=fruit.name}}
{{/each}}
<button {{action printWrappedFruits}}>Print</button>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2",
"ember-composable-helpers": "3.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment