rstacruz / jquery.transit

Super-smooth CSS3 transformations and transitions for jQuery
http://ricostacruz.com/jquery.transit
7.3k stars 864 forks source link

jQuery deferred support #162

Open incompl opened 10 years ago

incompl commented 10 years ago

...would be awesome, so I could write code like $.when(...).then(...) which seems very natural for animation code.

If this already exists, am I missing it in the documentation? Or perhaps should it be added to the README?

-Greg

incompl commented 10 years ago

Actually found that it works great, maybe worth putting this as an example in the README since it's a huge benefit to using a library like this.

Example:

var transition = $info.transition({top: '0'});

$.when(transition).then(function() {
  console.log('done!');
});
rpocklin commented 10 years ago

Thanks @incompl that works well. Would be nice to support .done(), .then() methods for async chaining without $.when(...)

rjgotten commented 10 years ago

Thanks @incompl that works well.

The piece of code @incompl posted does not do what he (and you) think it does and any callback you attach to your transition in this way will not be deferred until the transition finishes.

There is no promise implementation for $.fn.transition; it returns the original element set for chainability. That kind of object reference is a truthy value in boolean context and $.when resolves immediately when given a truthy value that does not implement jQuery's promise interface.

mhawk0 commented 10 years ago

Could it be reimplemented in a way $.animate behaves? That is also chainable (returns jQuery object, no BC break), but it is also possible to call .promise() on animated element, to return Promise object which can be used in $.when().

Expected behaviour:

$('.box')
  .transition({ x: -40 })
  .transition({ y: 40 })
  .transition({ x: 0 })
  .transition({ y: 0 });
$.when($('.box').promise(), function() {
  alert('all animations complete!');
});