As view logic increases, the indirection resulting from Handlebars’ hatred of logic & the verbosity of Ember’s component APIs becomes increasingly cumbersome. I wrote this little factory that accepts a hash of view model properties and returns a mixin that sets a component property and a corresponding setter action of the same name. This has greatly reduced the cognitive overhead of writing stateful components and reduced LOC.
I’d be interested in reactions to this — whether this is useful, pointless, or not-invented-here for Ember types. What do you think?
import Ember from 'ember';
// Pass in a hash of properties to return a mixin that provides properties & corresponding setter actions.
// See README for more.
export default input =>
Ember.Mixin.create({
...input,
actions : Object.keys( input )
.reduce( ( actions, key ) =>
Object.assign( actions, {
[ key ] : function( input ){
this.set( key, arguments.length ? input : !this.get( key ) )
}
} ),
{}
)
});