Skip to content

Instantly share code, notes, and snippets.

@Gaurav0
Created September 28, 2018 15:34
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 Gaurav0/86ed63a495dbc010a3e7bfb18e2a839a to your computer and use it in GitHub Desktop.
Save Gaurav0/86ed63a495dbc010a3e7bfb18e2a839a to your computer and use it in GitHub Desktop.
SO classNameBindings example
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['component'],
classNameBindings: [
'flag:emberClassA:emberClassB'
],
flag: false,
didRender() {
Ember.run.scheduleOnce('afterRender', this, this.restoreJQueryClass);
},
restoreJQueryClass() {
if (this._savedClassState) {
this.$().addClass('jQueryClass', this._savedClassValue);
}
},
actions: {
toggleFlag () {
this.toggleProperty('flag');
},
toggleClassFromOutside () {
this.$().toggleClass('jQueryClass');
this.set('_savedClassState', this.$().hasClass('jQueryClass'));
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<style>
.component {
min-height: 150px;
}
</style>
<p>
Button A toggles a class that changes the background color through the <code>classNameBindings</code> attribute
</p>
<p>
Button B uses jQuery to toggle a class that adds a green border.
</p>
<p>
If the class through jQuery is added and then the ember button is pressed, the jQuery class is lost.
</p>
{{x-example}}
<style>
.emberClassA {
background: red;
}
.emberClassB {
background: blue;
}
.jQueryClass {
border: 10px solid green;
}
</style>
<button type="button" {{action "toggleFlag"}}>
A) Toggle ember classNameBindings
</button>
<br />
<button type="button" {{action "toggleClassFromOutside"}}>
B) Change class with jQuery
</button>
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment