Ember Enumerology: Composable Computed Properties

We seem to spend a lot of time writing code in my Ember apps that looks a lot like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
App.CommentController = Em.ArrayController.extend

  numberOfComments: (-> @get('content.length') ).property('content.[]')

  byPopularity: (->
    # Have to manually implement our compare function because by default
    # JavaScript compares lexigraphically.
    @get('content').sort (a,b)->
      if a.get('upVotes') > b.get('upVotes')
        1
      else if a.get('upVotes') < b.get('upVotes')
        -1
      else
        0
  ).property('content.@each.upVotes')

i.e. manipulating and filtering collections of data in computed properties, so I thought it would be cool if there was a more composable way of creating these chains for computed properties.

Enter ember-enumerology, a small library to do just this. It let’s you write the following code:

1
2
3
App.CommentController = Em.ArrayController.extend
  numberOfComments: Enumerology.create('content').length().finalize()
  byPopularity:     Enumerology.create('content').sortNumericallyBy('upVotes').finalize()

Which seems like a pretty clear win for composition and reusability to me.

Comments