Skip to content

Instantly share code, notes, and snippets.

@zelaznik
Last active April 27, 2020 18:36
Show Gist options
  • Save zelaznik/b6b50e8f56e4cb92c1b4f80d4cea22c3 to your computer and use it in GitHub Desktop.
Save zelaznik/b6b50e8f56e4cb92c1b4f80d4cea22c3 to your computer and use it in GitHub Desktop.
Actions And Glimmer Components
import GlimmerComponent from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import EmberComponent from '@ember/component';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { addObserver } from '@ember/object/observers';
function myAction(_target, _name, descriptor) {
const original = descriptor.value;
const cache = {};
return {
get() {
const id = guidFor(this);
const context = this;
if (id in cache) {
return cache[id];
}
const value = function() {
return original.apply(context, arguments);
};
cache[id] = value;
window.context = context;
console.log(context);
// context.addObserver('willDestroyElement', function() {
// console.log("DESTROYING ELEMENT");
// delete cache[id];
// });
return value;
}
}
}
export default class extends GlimmerComponent {
@tracked isOpen
@myAction
toggleOpen() {
console.log(`wasOpen == ${this.isOpen}`);
this.isOpen = !this.isOpen;
console.log(`isOpen == ${this.isOpen}`);
}
}
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
<h1>Demonstraction of action decorator</h1>
<br>
<br>
<LocationPicker />
<br>
<br>
<div>
Modal is "{{if this.isOpen 'open' 'closed'}}"
<br><br>
<button {{on 'click' this.toggleOpen}}>
Click here to {{if this.isOpen 'close' 'open'}}
</button>
</div>
{
"version": "0.17.0",
"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.3.1/jquery.js",
"ember": "3.17.0",
"ember-template-compiler": "3.17.0",
"ember-testing": "3.17.0"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment