Created
November 14, 2014 07:09
-
-
Save manmal/4a9836922e11157d7ecf to your computer and use it in GitHub Desktop.
Ember Promised Properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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;` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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