Skip to content

Instantly share code, notes, and snippets.

@HeroicEric
Last active May 14, 2021 16:04
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 HeroicEric/ca5687d82e5d54a9ae07e3253a42da79 to your computer and use it in GitHub Desktop.
Save HeroicEric/ca5687d82e5d54a9ae07e3253a42da79 to your computer and use it in GitHub Desktop.
New Twiddle
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
const FRUITS = [
{ name: 'Apple' },
{ name: 'Banana' },
{ name: 'Figs' },
{ name: 'Orange' },
{ name: 'Pear' },
];
export default class ApplicationController extends Controller {
@tracked name;
@tracked favoriteFruit;
fruits = FRUITS;
@action
setName(event) {
this.name = event.target.value;
if (this.name === 'Adelle') {
this.favoriteFruit = this.fruits.find((fruit) => fruit.name === 'Figs');
}
}
@action
selectFavoriteFruit(event) {
let selectedFruitName = event.target.value;
this.favoriteFruit = this.fruits.find((fruit) => fruit.name === selectedFruitName);
}
}
{{#if this.name}}
<h1>Welcome {{this.name}}!</h1>
{{#if this.favoriteFruit}}
<p>I heard you're a fan of {{this.favoriteFruit.name}}.</p>
{{/if}}
{{/if}}
{{outlet}}
<p>
Fill in the name with "Adelle" ;)
</p>
<div>
<label>
Name
<input
type="text"
value={{this.name}}
{{on "input" this.setName}}
/>
</label>
</div>
{{#if this.name}}
<div>
<label>
Favorite fruit
<select {{on "change" this.selectFavoriteFruit}}>
<option value="">Select a fruit</option>
{{#each this.fruits as |fruit|}}
<option
selected={{eq this.favoriteFruit.name fruit.name}}
value={{fruit.name}}
>
{{fruit.name}}
</option>
{{/each}}
</select>
</label>
</div>
{{/if}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-truth-helpers": "3.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment