Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Created April 29, 2014 21:14
Show Gist options
  • Select an option

  • Save rmosolgo/88139e743bc0f74f5a35 to your computer and use it in GitHub Desktop.

Select an option

Save rmosolgo/88139e743bc0f74f5a35 to your computer and use it in GitHub Desktop.
Batman.js calcuated properties over collections
class App.Sale extends Batman.Model
@hasMany 'sale_items'
@accessor 'total_price', ->
memo = 0
# adds 'sale_items' and each item as a source
@get('sale_items').forEach (item) ->
memo += item.get('subtotal')
memo
class App.SaleItem extends Batman.Model
# adds quantity and price as sources
@accessor 'subtotal', -> @get('quantity') * @get('price')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment