Skip to content

Instantly share code, notes, and snippets.

@ASH-Bryan
Last active July 16, 2020 17:32
Show Gist options
  • Save ASH-Bryan/8f08a34909eed56380913678f480321b to your computer and use it in GitHub Desktop.
Save ASH-Bryan/8f08a34909eed56380913678f480321b to your computer and use it in GitHub Desktop.
HTML proxy
import Component from '@ember/component';
import layout from '../templates/components/html-button';
import { observer } from '@ember/object';
export default Component.extend({
layout,
isVisible: false,
isDisabled: false,
clickAction: () => null,
visibility: observer('isVisible', function() {
this._updateElement()
}),
disabled: observer('isDisabled', function() {
this._updateElement()
}),
didInsertElement() {
this._getElement().addEventListener('click', this.clickAction, false)
this._updateElement()
},
willDestroyElement() {
this._getElement().removeEventListener('click', this.clickAction, false)
},
_getElement() {
return document.getElementById('enroll')
},
_updateElement() {
this._getElement().style.display = this.isVisible ? 'block' : 'none'
this._getElement().disabled = this.isDisabled
},
})
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'HTML Proxy'
}
import Controller from '@ember/controller';
export default Controller.extend({
isVisible: true,
isDisabled: false,
actions: {
buttonAction() {
alert('Action from Ember')
},
},
});
<h2>Plain HTML element</h2>
<button id="enroll" style="display: none;">Enroll</button>
<h2>Interact with Ember component</h2>
{{html-button
isVisible=isVisible
isDisabled=isDisabled
clickAction=(action 'buttonAction')
}}
<label>
isVisible:
{{input type="checkbox" name="isVisible" checked=isVisible}}
</label>
<label>
isDisabled:
{{input type="checkbox" name="isDisabled" checked=isDisabled}}
</label>
{{!--
Might look something like this in the final
{{#ash-header as |header|}}
{{header.button
text="Register"
clickAction=(action 'registerButton')
isVisible=isVisible
isDisabled=isDisabled
}}
{{header.button
text="Login"
clickAction=(action 'loginButton')
isVisible=isVisible
isDisabled=isDisabled
}}
{{/ash-header}}
--}}
{
"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.8.3",
"ember-template-compiler": "3.8.3",
"ember-testing": "3.8.3"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment