This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_backbone_marionette_transition_region.js
80 lines (61 loc) · 3.51 KB
/
custom_backbone_marionette_transition_region.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(function() {
define([
'backbone.marionette',
'lib/transition_adapter'
],
function(Marionette){
Backbone.Marionette.AnimatedRegion = Backbone.Marionette.Region.extend({
// Very similar to show(), but uses css transition class between views
// @param newView - this is the view that will replace the current view in the region
// @param type - the type of animation to use while transitioning the newView in. Accepts 'slide' and 'fade'. Defaults to 'slide'
// @param direction - the direction of the 'slide' animation. Accepts 'left', 'right', 'down', 'up'. Defaults to 'left'. Only works for 'slide' type animation
transitionToView: function(newView, options) {
var self = this;
// If options is undefined, define an object clone to setup defaults later
// If options is defined, go ahead and shallow clone it
// We need to clone the object becuase of the way Javascript passes the options
// by reference. If we didn't clone the options object, the changes we make to options
// could affect other calls using the same options object
// For example, if we called the following without using a cloned options object here,
// The _.defaults function below would mutate the options object for the second caller.
//
// App.headerRegion.transitionToView(headerView, options);
// App.mainRegion.transitionToView(lessonsView, options); // this would get a mutated options object passed in because the first instance would add properties to 'options' that we don't need the second time 'options' is passed in.
if (typeof options == 'undefined') {
optionsClone = {};
} else {
optionsClone = _.clone(options);
}
_.defaults(optionsClone, {
type: 'slide',
direction: 'left',
transitionEndCb: function() {
// clean up the old view
self.close();
self.currentView = newView;
// do the things show would normally do after showing a new view
Backbone.Marionette.triggerMethod.call(newView, "show");
Backbone.Marionette.triggerMethod.call(self, "show", newView);
}
});
// Do we have a view currently?
var view = this.currentView;
// If we don't currently have a view, simply show the newView and exit
if (!view || view.isClosed){
this.show(newView);
return;
}
view.triggerMethod('willTransition');
// Tell this region to stop listening for 'render' on the newView
this.stopListening(newView, 'render');
// Wait for the new view to render, then initialize a transition to
// show the new view while hiding the old.
this.listenTo(newView, 'render', function() {
Backbone.Marionette.Transition.transitionEl(self.$el, newView.$el, optionsClone);
}); // end listenTo
newView.render();
} // end transitionToView
});
return Backbone.Marionette.AnimatedRegion;
});
}).call(this);