Phil Gengler


on software, development, and anything else

Introducing ember-property-computed

Today I released ember-property-computed, a small ember-cli addon to allow specifying computed properties by giving the function first and then the dependent keys.

Basically, in Ember there are two ways of specifying computed properties:

aliasPropertyName: function() {
  // do something
}.property('dependentKey')

and

aliasPropertyName: Ember.computed('dependentKey', function() {
 // do something
}

Notice that the two styles have the ordering of the two parts (the function and the list of dependent keys) switched; with .property() the function appears first, followed by the keys, while with Ember.computed the dependent keys are specified first.

What ember-property-computed does is provide a compromise between the two; it isn't a prototype extension so it's safe to use in addons*, but it preserves the function-then-keys ordering.

To use it, install the ember-property-computed addon in your project by running ember install ember-property-computed. Then, in any file where you want to define computed properties, import the function:

import cp from 'ember-property-computed';

You can replace cp with whatever name you want to use for the function; I chose cp because it's short and stands for "computed property".

Then, when you want to define a computed property, use the function like this:

//...
computedPropertyName: cp(function() {
  /* do whatever here */
}, 'dependentKeyName'),
//...