Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Last active June 21, 2020 15:11
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 NullVoxPopuli/23c5a7efdb605d7b5fa9cd9da61c1294 to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/23c5a7efdb605d7b5fa9cd9da61c1294 to your computer and use it in GitHub Desktop.
StackOverflow 60055682 but with tracked-built-ins
<section>
<h2>items in the component</h2>
<ul>
{{#each this.items as |item|}}
<li>
{{item.quantity}} x {{item.name}}
<button class='button-sm' {{on 'click' (fn this.add item)}}>
Add Item
</button>
<button class='button-sm' {{on 'click' (fn this.remove item)}}>
Remove Item
</button>
</li>
{{/each}}
</ul>
</section>
<section>
<h2>items in the service</h2>
<ul>
{{#each this.cart.items as |item|}}
<li>
{{item.quantity}} x {{item.name}}
</li>
{{/each}}
</ul>
</section>
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { action, set } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { TrackedArray } from 'tracked-built-ins';
// Note that name and quantity are *not* tracked
class Item {
name;
quantity;
constructor(id, name, quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}
}
export default class DemoRoot extends Component {
@service cart;
@tracked items;
constructor(owner, args) {
super(owner, args);
// Setup some default data for the demo
this.items = [
new Item(1, 'Apple', 1),
new Item(2, 'Orange', 1),
new Item(3, 'Carolina Reaper', 1),
];
this.cart.items = [...this.items];
}
@action
add(item) {
for (let obj of this.items) {
if (obj.id !== item.id) continue;
// set must be used because quantity is not known to the tracking system
set(obj, 'quantity', obj.quantity + 1);
}
}
@action
remove(item) {
for (let obj of this.items) {
if (obj.id !== item.id) continue;
// set must be used because quantity is not known to the tracking system
set(obj, 'quantity', obj.quantity - 1);
}
}
}
<h1>
Answer to
<Link href="https://stackoverflow.com/questions/60055682/">
StackOverflow #60055682
</Link>
</h1>
<em>Ember 3.16+</em>
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
import { helper } from '@ember/component/helper';
export default helper(function asFormattedString([a]/*, hash*/) {
return a ? JSON.stringify(a, null, 2) : `${a}`;
});
import { helper } from '@ember/component/helper';
export default helper(function eq([a, b]/*, hash*/) {
return a === b;
});
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { TrackedArray } from 'tracked-built-ins';
export default class CartService extends Service {
@tracked items = new TrackedArray([]);
}
body {
padding: 1rem;
}
li {
margin: 0.5rem 0;
}
<link rel="stylesheet" href="https://cdn.shoelace.style/1.0.0-beta.25/shoelace.css">
<Title />
<br><br>
<Demo />
{
"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-modifier": "1.0.3",
"tracked-built-ins": "1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment