Skip to content

Instantly share code, notes, and snippets.

@manmal
Created November 14, 2014 07:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manmal/4a9836922e11157d7ecf to your computer and use it in GitHub Desktop.
Save manmal/4a9836922e11157d7ecf to your computer and use it in GitHub Desktop.
Ember Promised Properties
# /app/controllers/comment.coffee
`import promisedProperty from "../utils/promised_property"`
CommentController = Ember.ObjectController.extend
needs: ["application"]
currentUser: Ember.computed.alias('controllers.application.currentUser')
isCommentController: true
currentCommentUser: promisedProperty(false, -> @get('model.user')
).property('model.user')
isCurrentUser: (->
@get('currentUser') == @get('currentCommentUser')
).property('currentUser', 'currentCommentUser')
isActionsHidden: Ember.computed.alias('isCurrentUser')
actions:
saveTextField: (propertyName) ->
@get('model').saveShadowedProperty(propertyName)
deleteComment: ->
@get('model').destroyRecord()
`export default CommentController;`
# /app/utils/promised_property.coffee
# http://discuss.emberjs.com/t/promises-and-computed-properties/3333/10
promisedProperty = (initialValue, func) ->
flightKey = '_promisedInFlight' + Ember.guidFor({})
(k,v) ->
return v if arguments.length > 1
this[flightKey] ?= 0
this[flightKey] += 1
myNumber = this[flightKey]
func.apply(this, [k,v]).then((result) =>
if this[flightKey] <= myNumber
@set(k, result)
this[flightKey] -= 1
)
initialValue
`export default promisedProperty;`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment