Skip to content

Instantly share code, notes, and snippets.

@krisselden
Last active April 27, 2016 20:20
Show Gist options
  • Save krisselden/6bff3f203d11ef01ec51b716865f5385 to your computer and use it in GitHub Desktop.
Save krisselden/6bff3f203d11ef01ec51b716865f5385 to your computer and use it in GitHub Desktop.
Complex Prop Bindings
import Ember from 'ember';
export default Ember.Component.extend({
attributeBindings: ['style'],
style: Ember.computed('width', 'height', 'color', function() {
let width = this.get('width');
let height = this.get('height');
let color = this.get('color');
return 'width:'+width+'px;height:'+height+'px;background-color:'+color;
}),
color: 'red',
height: 20,
width: Ember.computed('height', {
get() {
return this.get('height')*2;
},
set(keyName, width) {
this.set('height', width/2);
return width;
}
})
});
import MyComponent from './my-component';
export default MyComponent.extend({
color: 'yellow',
init() {
this._super(...arguments);
this.height = 150;
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
height: 60,
width: 80
});
<h1>Welcome to {{appName}}</h1>
<h2>More complex component prop bindings</h2>
<h3>Dynamic bound CP</h3>
{{input type="range" min=20 max=100 value=height}}
{{#my-component height=height color="#ccc"}}
hello
{{/my-component}}
<h3>Defaults from prototype</h3>
{{my-component}}
<h3>2-way bound CP setter</h3>
{{input type="range" min=20 max=100 value=width}}
{{my-component width=width color="blue"}}
<h3>Overriden prop + plus initialized in create</h3>
{{my-subcomponent}}
{{my-subcomponent color="green"}}
{{outlet}}
<br>
<br>
{
"version": "0.7.2",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "1.12.2",
"ember-data": "1.13.15",
"ember-template-compiler": "1.12.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment