Skip to content

Instantly share code, notes, and snippets.

@kmiyashiro
Forked from sduquej/controllers.application.js
Last active February 12, 2020 04:00
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 kmiyashiro/ea3ff35f5e905f6d8cdab593f32e8e8c to your computer and use it in GitHub Desktop.
Save kmiyashiro/ea3ff35f5e905f6d8cdab593f32e8e8c to your computer and use it in GitHub Desktop.
Editing array of strings
import Ember from 'ember';
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'));
}
});
<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 as |fruit index|}}
{{get primitiveFruits index}}
{{input value=(mut (get primitiveFruits 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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment