Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kumkanillam/5cbe39252b7e523f4c74299dd6f83015 to your computer and use it in GitHub Desktop.
Save kumkanillam/5cbe39252b7e523f4c74299dd6f83015 to your computer and use it in GitHub Desktop.
Simple Decorator Pattern
import Ember from 'ember';
import MyModelDecorator from '../decorators/my-model';
export default Ember.Component.extend({
decoratedPerson: Ember.computed('person', {
get() {
// If you needed dependency injection you would need to
// let MyModelDecorator = Ember.getOwner(this).factoryFor('decorator:my-model');
// OR
// let MyModelDecorator = Ember.getOwner(this)._lookupFactory('decorator:my-model');
// Depending on your version of Ember.
return MyModelDecorator.create({content: this.get('person')});
}
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
model: Ember.computed({
get() {
return this.get('store').createRecord('my-model', {
firstName: 'Bob',
lastName: 'NoName'
});
}
})
});
import Ember from 'ember';
export default Ember.ObjectProxy.extend({
isAwesome: Ember.computed.equal('content.firstName', 'Jane'),
awesomeStyle: Ember.computed('isAwesome', {
get() {
let style = this.get('isAwesome') ? 'background-color: gold;' : '';
return Ember.String.htmlSafe(style);
}
})
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
export default Model.extend({
firstName: attr('string'),
lastName: attr('string')
});
<p>Try <code>Jane</code>.</p>
<input type="text" value={{model.firstName}} oninput={{action (mut model.firstName) value="target.value"}}>
{{my-component person=model}}
<span style={{decoratedPerson.awesomeStyle}}>
{{decoratedPerson.firstName}}
</span>
{{decoratedPerson.lastName}}
{{#if decoratedPerson.isAwesome}}
is awesome!
{{/if}}
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment