Skip to content

Instantly share code, notes, and snippets.

@cha55son
Created March 19, 2014 20:10
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 cha55son/9650155 to your computer and use it in GitHub Desktop.
Save cha55son/9650155 to your computer and use it in GitHub Desktop.
Durandal Transition Complete
/**
* The entrance transition module.
* @module entrance
* @requires system
* @requires composition
* @requires jquery
*/
define(['durandal/system', 'durandal/composition', 'jquery'], function(system, composition, $) {
var fadeOutDuration = 100;
var endValues = {
marginRight: 0,
marginLeft: 0,
opacity: 1
};
var clearValues = {
marginLeft: '',
marginRight: '',
opacity: '',
display: ''
};
/**
* @class EntranceModule
* @constructor
*/
var entrance = function(context) {
return system.defer(function(dfd) {
function endTransition() {
context.model.transitionComplete && context.model.transitionComplete(context.child, context.parent);
dfd.resolve();
}
function scrollIfNeeded() {
if (!context.keepScrollPosition) {
$(document).scrollTop(0);
}
}
if (!context.child) {
$(context.activeView).fadeOut(fadeOutDuration, endTransition);
} else {
var duration = context.duration || 500;
var fadeOnly = !!context.fadeOnly;
function startTransition() {
scrollIfNeeded();
context.triggerAttach();
var startValues = {
marginLeft: fadeOnly ? '0' : '20px',
marginRight: fadeOnly ? '0' : '-20px',
opacity: 0,
display: 'block'
};
var $child = $(context.child);
var animate = function() {
$child.animate(endValues, {
duration: duration,
easing: 'swing',
always: function () {
$child.css(clearValues);
endTransition();
}
});
};
$child.css(startValues);
var promise = context.model.transitionStart && context.model.transitionStart(context.child, context.parent);
if (promise && promise.then)
promise.then(function() {
animate();
});
else
animate();
}
if (context.activeView) {
$(context.activeView).fadeOut({ duration: fadeOutDuration, always: startTransition });
} else {
startTransition();
}
}
}).promise();
};
return entrance;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment