Skip to content

Instantly share code, notes, and snippets.

@njoyard
Created October 29, 2020 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 njoyard/6646838412ea59a44bee8bf97defba2f to your computer and use it in GitHub Desktop.
Save njoyard/6646838412ea59a44bee8bf97defba2f to your computer and use it in GitHub Desktop.
EmberArray autotracking
import { A } from '@ember/array';
import Controller from '@ember/controller';
import { action } from '@ember/object';
export default class ApplicationController extends Controller {
items = A([]);
get even() {
return this.items.length % 2 === 0;
}
get len() {
return this.items.length;
}
get allNine() {
return this.items.every((i) => i.includes('9'));
}
get someNine() {
return this.items.some((i) => i.includes('9'));
}
@action
add() {
this.items.pushObject(`${Math.floor(Math.random() * 100000)}`);
}
}
<button {{on 'click' this.add}}>add</button>
{{#each this.items as |item|}}
<div>{{item}}</div>
{{/each}}
<div>from array.length: {{this.items.length}} items</div>
<div>from len: {{this.len}} items</div>
{{#if this.allNine}}<div>all items have 9s</div>{{/if}}
{{#if this.someNine}}<div>some items have 9s</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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment