Created
July 18, 2013 14:05
-
-
Save cancer/6029586 to your computer and use it in GitHub Desktop.
Angular.jsでDeferredしたくて頑張ってみたらcss animationが綺麗に書けた ref: http://qiita.com/cancer/items/57ac6b3b0b5d04ed74d6
This file contains 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
myDeferredA = (state) -> | |
deferred = $q.defer() | |
successMsg = 'Success!' | |
errorMsg = 'Error!' | |
setTimeout( -> | |
$scope.$apply -> | |
if state == 0 | |
deferred.resolve successMsg | |
else | |
deferred.reject errorMsg | |
, 3000) | |
deferred.promise | |
promiseA = myDeferredA(Math.random()*2|0) | |
promiseA.then (msg) -> | |
console.log "success: #{msg}" | |
, (msg) -> | |
console.log "error: #{msg}" |
This file contains 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
$q.when(myDeferredA, myDeferredB, myDeferredC).then -> | |
console.log "all promises are completed!!" | |
# | |
# myDefferedA#resolve() | complete | | |
# myDefferedB#resolve() | complete | -> success | |
# myDefferedC#resolve() | complete | | |
# |
This file contains 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
$q.when(myDeferredA, myDeferredB, myDeferredC).then -> | |
console.log "some one promise is completed!!" | |
# | |
# myDefferedA#resolve() | -> not complete | | |
# myDefferedB#resolve() | -> complete | -> success | |
# myDefferedC#resolve() | -> not complete | | |
# |
This file contains 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
animation = cssAnimation.create element | |
animation | |
.end(-> | |
... | |
).end(-> | |
... | |
).end |
This file contains 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
class Animation | |
constructor: (elem) -> | |
css = $window.getComputedStyle elem[0], '' | |
duration = css[supportAnimation.duration] || '0s' | |
@elem = elem | |
@_msec = +duration * 1000 | |
@_queue = [] | |
@_isProgress = false | |
@_promise = null |
This file contains 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
# eventをbindしてdeferオブジェクトを返す | |
set: (event, callback, isReturnPromise) -> | |
isReturnPromise = false unless isReturnPromise? | |
_dfdFunc = () => | |
dfd = $q.defer() | |
@elem.bind event, => | |
@elem.unbind event, callback | |
# callbackの中でresolve/rejectできるようにしておく | |
callback.call(@, dfd) | |
dfd.resolve() unless isReturnPromise | |
return dfd.promise | |
@done _dfdFunc | |
return @ | |
# animationEnd用のsetラッパー | |
end: (callback, isReturnPromise) -> | |
return @set supportAnimation.end, callback, isReturnPromise |
This file contains 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
supportAnimation = do -> | |
el = document.createElement 'tada' | |
transEndEventNames =- | |
'WebkitAnimation' : 'webkitAnimationEnd' | |
'animation' : 'animationend' | |
transPropNames =- | |
'WebkitAnimation' : 'webkitAnimationDuration' | |
'animation' : 'animationDuration' | |
for name of transEndEventNames | |
if el.style[name] isnt undefined | |
return { | |
end : transEndEventNames[name], | |
duration : transPropNames[name] | |
} |
This file contains 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
# promiseのsuccessCallbackに`callback`を追加する | |
# promiseが無ければ`callback`を実行して、promiseを作る | |
done: (callback) -> | |
if @_promise == null | |
@_promise = callback.call(@) | |
else | |
@_promise = @_promise.then(callback) |
This file contains 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
return { | |
create: (elem) -> | |
new Animation(elem) | |
} |
This file contains 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
module.controller 'hoge', ['cssAnimation', (cssAnimation) -> | |
element = angular.element document.querySelector '.animation' | |
animation = cssAnimation.create element | |
animation | |
.end(-> | |
console.log '1st animation is ended' | |
).end(-> | |
console.log '2nd animation is ended' | |
).end((dfd) -> | |
$timeout(-> | |
console.log '3rd animation is ended' | |
dfd.resolve() | |
, 3000) | |
, true).done(-> | |
console.log 'all animations are ended' | |
) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment