Last active
June 21, 2020 15:11
-
-
Save NullVoxPopuli/23c5a7efdb605d7b5fa9cd9da61c1294 to your computer and use it in GitHub Desktop.
StackOverflow 60055682 but with tracked-built-ins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function asFormattedString([a]/*, hash*/) { | |
return a ? JSON.stringify(a, null, 2) : `${a}`; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function eq([a, b]/*, hash*/) { | |
return a === b; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
padding: 1rem; | |
} | |
li { | |
margin: 0.5rem 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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