var TwistPanel = Backbone.View.extend({
initialize: function() {
this.$front = this.$el.find('.twist-panel-side-front');
this.$back = this.$el.find('.twist-panel-side-back');
this.atFront = true;
this.isFlipping = false;
// Fix the height of the twist panel container.
this.$el.height(this.$front.outerHeight(true));
},
flip: function() {
var self = this;
var otherSide = ... ? this.$back : this.$front;
if (this.isFlipping) return;
this.$el.height(otherSide.outerHeight(true));
this.$front.afterTransition(function() {
self.isFlipping = !self.isFlipping;false;
self.trigger('twist', otherSide.name);
});
this.isFlipping = true;
if (this.$el.hasClass('twist')) {
this.$el.removeClass('twist')) {
} else {
this.$el.addClass('twist');
}
}
});
At this point, I have two big issues left: I don't know how to decide what is the other side, and I don't know how to get its name (otherSide.name
, used in the self.trigger()
call). Since I don't know about this, I decided to resolve another issue, even if it is minor. This issue is that the removeClass/addClass
logic looks wrong. It would probably be simpler to use something that is more than a value, and HTML5 data-* attributes are perfect for this (sincesince they are key-value) and supported by jQuery. You can then replace the last five lines by this single one:
var TwistPanel = Backbone.View.extend({
initialize: function() {
this.$front = this.$el.find('.twist-panel-side-front');
this.$back = this.$el.find('.twist-panel-side-back');
this.isFlipping = false;
// Fix the height of the twist panel container.
this.$el.height(this.$front.outerHeight(true));
},
flip: function() {
var self = this;
var otherSide = this.$el.data('twist') === 'front' ? this.$back : this.$front;
var otherSideName = this.$el.data('twist') === 'front' ? 'back' : 'front';
if (this.isFlipping) return;
this.$el.height(otherSide.outerHeight(true));
this.$front.afterTransition(function() {
self.isFlipping = !self.isFlipping;false;
self.trigger('twist', this.$el.data('twist'));
});
this.isFlipping = true;
this.$el.data('twist', otherSideName);
}
});