Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Implementing the `comments` property in CoffeeScript.
# Note the lack of `flatten` and the heavy guarding
# against objects being undefined.
App.Blog = Ember.Object.extend
comments: (->
# Return all comments of all posts by flattening.
   (@get('posts.@each.comments') ? []).reduce (a, b) ->
# b is a lazy hasMany collection of comments, so we must cast to array.
(a ? []).concat((b ? []).toArray())
, []
).property('posts.@each.comments')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment