Skip to content

Instantly share code, notes, and snippets.

@superlou
Last active December 22, 2015 12:18
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 superlou/6471462 to your computer and use it in GitHub Desktop.
Save superlou/6471462 to your computer and use it in GitHub Desktop.
Getting attributes before they're resolved
attr = Ember.attr
hasMany = Ember.hasMany
Library.Book = Ember.Model.extend
id: attr(Number)
code: attr()
volume: attr()
stock: attr(Number)
title: attr()
adult: attr()
notes: attr()
checkouts: hasMany('Library.Checkout',{key: 'checkout_ids'})
available: (->
copies_out = @get('checkouts').filterProperty('status', 'out').get('length')
@get('stock') - copies_out
).property('stock', 'checkouts.@each.status')
full_title: (->
volume = @get('volume')
if volume
@get('title') + ", vol. " + volume
else
@get('title')
).property('title', 'volume')
Library.Book.reopenClass
findByCode: (code) ->
@find('code:' + code)
cloneVolumeFrom: (original_id) ->
@fetch(original_id).then (item)->
cloned_book = Library.Book.create
title: item.get('title')
notes: item.get('notes')
return cloned_book
Library.Book.url = '/books'
Library.Book.adapter = Library.RESTAdapter.create()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment