Skip to content

Instantly share code, notes, and snippets.

@joliss
Created October 16, 2012 18:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joliss/3901154 to your computer and use it in GitHub Desktop.
# 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