Skip to content

Instantly share code, notes, and snippets.

@ankushdharkar
Last active September 21, 2020 18:14
Show Gist options
  • Save ankushdharkar/bfdfe4da3231671da3082a536e456197 to your computer and use it in GitHub Desktop.
Save ankushdharkar/bfdfe4da3231671da3082a536e456197 to your computer and use it in GitHub Desktop.
Using Array in Ember Octane
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { A } from '@ember/array';
import { set } from '@ember/object';
class Age {
@tracked value;
constructor(val) {
this.value = val;
}
}
export default class ApplicationController extends Controller {
usersListData = A([
{
firstName: 'Ankush',
lastName: 'Dharkar',
age: new Age(30)
},
{
firstName: 'Sindbad',
lastName: 'the Sailor',
age: new Age(40)
}
])
@action
addNewUser () {
this.usersListData.pushObject({
firstName: 'David',
lastName: 'Kopp',
age: new Age(45)
});
}
@action
changeFirstname () {
const obj = this.usersListData.objectAt(0);
// [1.]
obj.age.value = 22;
// [2.]
set(obj, 'firstName', 'Taksh');
set(obj, 'lastName', 'Channa');
/*
In order to make sure the context updates properly, you must invalidate the property when updating it.
1. You can mark the property as `@tracked`, or
2. Use `@ember/object#set`: https://api.emberjs.com/ember/3.21/functions/@ember%2Fobject/set
*/
}
}
{{#each this.usersListData as |userItem|}}
{{log userItem.age}}
<h2>{{userItem.firstName}} {{userItem.lastName}} = {{userItem.age.value}}</h2>
{{/each}}
<button type="button" {{on "click" this.addNewUser}} >Add New User</button>
<button type="button" {{on "click" this.changeFirstname}}>Change 1st user</button>
{
"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