Skip to content

Instantly share code, notes, and snippets.

@meelash
Created June 14, 2013 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meelash/5785328 to your computer and use it in GitHub Desktop.
Save meelash/5785328 to your computer and use it in GitHub Desktop.
Workaround of issues with chaining/nesting under @each in computed properties/ bound observers: https://github.com/emberjs/ember.js/issues/541
I wanted to do something like this:
App.ApplicationController = Ember.Controller.extend
selectedAccounts: (->
accounts = []
(@get 'currentUser.entities').forEach (entity)->
accounts.pushObjects entity.get('accounts').filterProperty 'selected'
accounts
).property('currentUser.entities.@each.accounts.@each.selected')
To workaround, I did this:
App.ApplicationController = Ember.Controller.extend
selectedAccounts: (->
accounts = []
(@get 'currentUser.entities').forEach (entity)->
accounts.pushObjects entity.get('selectedAccounts')
accounts
).property('currentUser.entities.@each.selectedAccounts')
App.Entity = DS.Model.extend
selectedAccounts: (->
(@get 'accounts').filterProperty 'selected'
).property 'accounts.@each.selected'
@joliss
Copy link

joliss commented Aug 28, 2013

Yes, this is pretty much what we ended up doing as well; but note that this will likely still give you horrible quadratic performance, because of the way changes propagate. In other words, as soon as you throw more than a handful of accounts at it, it will grind to a halt.

The usual solution is to serve down the selectedAccounts list from the server, instead of trying to compute it.

(Single @each is bad already, but I seem to recall that simulating nested @each gets you tangled up even worse.)

@joliss
Copy link

joliss commented Aug 28, 2013

Hah, just ran into some fresh work that's been done on array CP performance:

https://github.com/emberjs/ember.js/tree/master/packages/ember-runtime/lib/computed
emberjs/ember.js@fc6e2f5

@joliss
Copy link

joliss commented Aug 29, 2013

See also http://emberjs.com/blog/2013/08/29/ember-1-0-rc8.html , section Array Computed on how to get these things working without ruining performance (hopefully).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment