Skip to content

Instantly share code, notes, and snippets.

@les2
Last active October 9, 2016 18:42
Show Gist options
  • Save les2/9f1fc37e10e555858fa11abbc4ee5617 to your computer and use it in GitHub Desktop.
Save les2/9f1fc37e10e555858fa11abbc4ee5617 to your computer and use it in GitHub Desktop.
Attribute Bindings
import Ember from 'ember';
export default Ember.Component.extend({
ariaControls: null,
children: null,
api: null,
init() {
this._super(...arguments);
this.set('children', Ember.A());
this.set('api', {
register: this.register.bind(this),
});
},
register(child) {
this.get('children').pushObject(child);
if (this.get('active') === child.name) {
child.setActiveState(true);
}
},
// observer the active property
activateSelected: Ember.observer('active', function() {
let childApi = this.get('children').findBy('name', this.get('active'));
this.get('children').forEach(child => child.setActiveState(false));
childApi.setActiveState(true);
}),
});
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['nested-component'],
tagName: 'button',
attributeBindings: ['ariaSelected:aria-selected', 'ariaControls:aria-controls'],
classNameBindings: [
'active:my-active-class:my-inactive-class',
],
api: null,
active: false,
parentApi: null,
name: null,
demonstrateBug: true,
ariaSelected: Ember.computed('active', 'demonstrateBug', function() {
if (this.get('demonstrateBug')) {
return this.get('active');
}
return this.get('active') ? 'true' : 'false';
}),
init() {
this._super(...arguments);
Ember.assert('name required', this.get('name'));
this.set('api', {
name: this.get('name'),
setActiveState: this.setActiveState.bind(this),
});
this.get('parentApi').register(this.get('api'));
},
setActiveState(value) {
this.set('active', Boolean(value));
},
click() {
this.get('onselect')(this.get('name'));
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
active: 'first',
showBug: true,
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.nested-component {
position: relative;
padding: 20px;
margin: 20px;
background-color: transparent;
}
.nested-component[aria-selected=""] {
background-color: yellow;
}
.nested-component[aria-selected="true"] {
background-color: green;
color: yellow;
}
.nested-component[aria-selected="false"] {
background-color: blue;
color: while;
}
.nested-component.my-active-class {
border: 10px solid green;
}
.nested-component.my-active-class:before {
position: absolute;
display: block;
content: '⇩ Active ⇩';
top: -40px;
left: 0;
background-color: green;
color: yellow;
}
.nested-component[aria-selected=""]:after {
position: absolute;
display: block;
white-space: nowrap;
content: '⇧ aria-selected=""';
bottom: -40px;
left: 0;
background-color: green;
color: yellow;
}
.nested-component.my-inactive-class {
border: 10px solid blue;
}
<h1>Demonstrating a regression with attribute binding</h1>
<p>
Regression starts with ember 2.7.
</p>
<p>
If the "active" object has a GREEN background, it works. if the "active" object has a YELLOW backgrond, it's broken.
<pre>.nested-component[aria-selected="true"] {
background-color: green;
color: yellow;
}</pre>
</p>
<p></p>
<p>Demonstrate Bug? {{input type='checkbox' checked=showBug}}</p>
{{#my-component
active=(readonly active)
onselect=(action (mut active))
as |foo|
}}
{{#foo.nested demonstrateBug=showBug name='first'}}First{{/foo.nested}}
{{#foo.nested demonstrateBug=showBug name='second'}}Second{{/foo.nested}}
{{/my-component}}
<br>
<br>
{{yield (hash
nested=(component 'nested-component'
parentApi=api
onselect=(action onselect)
)
)
}}
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.10.5",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "beta",
"ember-data": "2.8.0",
"ember-template-compiler": "beta",
"ember-testing": "beta"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment