Skip to content

Instantly share code, notes, and snippets.

@22a
Created July 27, 2020 13:42
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 22a/872f72a918ea1b90f4f95a9286350eda to your computer and use it in GitHub Desktop.
Save 22a/872f72a918ea1b90f4f95a9286350eda to your computer and use it in GitHub Desktop.
tracked-property-index
import Component from '@ember/component';
import EmberObject, { computed } from '@ember/object';
export default Component.extend({
nums: undefined,
errors: undefined,
init() {
this._super(...arguments);
this.set('nums', [1,2,3,4,5]);
this.set('errors', EmberObject.create());
setTimeout(this.validateNums.bind(this), 100);
},
validateNums() {
console.log('validating numbers');
this.set('nums', this.nums.map((link, index) => {
if (link % 2 === 0) {
this.errors.set(index, 'bad number, divisible by 2');
} else {
this.errors.set(index, null);
}
return link;
}));
}
})
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
<h1>Welcome to {{this.appName}}</h1>
<MyComponent/>
{{#each this.nums as |num index|}}
<p>
{{num}} : {{get this.errors index}}
</p>
{{/each}}
---
{{#each this.nums as |num index|}}
{{#let (get this.errors index) as |error|}}
<p>
{{num}} : {{error}}
</p>
{{/let}}
{{/each}}
{
"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