Skip to content

Instantly share code, notes, and snippets.

@majornorth
Last active August 29, 2015 14:16
Show Gist options
  • Save majornorth/9f5c82a828657a45911d to your computer and use it in GitHub Desktop.
Save majornorth/9f5c82a828657a45911d to your computer and use it in GitHub Desktop.
My first Backbone feature
// Before
var ExplanationReviewForm = Backbone.View.extend({
ENDPOINTS: {},
$form: null,
submitted: false,
positiveReview: false,
events: {
'click .thumbs-up': 'submitPositiveReview',
'click .thumbs-down': 'showNegativeReviewForm',
'click .skip-review': 'submitReviewSkip',
'click .btn-primary': 'submitReview'
},
initialize: function() {
this.ENDPOINTS = {
'create-event': this.$el.attr('data-id') + 'add-review/',
'feedback': this.$el.attr('data-id') + 'review/'
};
this.$form = this.$('form');
},
showNegativeReviewForm: function(event) {
this.positiveReview = false;
this.$('.label-thumbs-down').show();
this.showReviewForm(event);
},
showReviewForm: function(event) {
event.preventDefault();
this.$form.show();
},
submitPositiveReview: function(event) {
this.positiveReview = true;
this.submitReview(event);
},
submitReviewSkip: function(event) {
this.$form.find('textarea').val('');
this.submitReview(event);
},
submitReview: function(event) {
event.preventDefault();
this.$form.hide();
if (this.submitted) {
return false;
}
this.submitted = true;
util.API.post(this.ENDPOINTS['create-event'], {
data: {
'is_helpful': this.positiveReview,
'text': this.$form.find('textarea').val()
},
csrf: true,
success: function() {
if (this.positiveReview) {
this.$el.removeClass()
.html('<h3>Thanks for your feedback!</h3>');
} else {
location.href = this.ENDPOINTS.feedback;
}
}.bind(this)
});
}
});
// After
var ExplanationReviewForm = Backbone.Marionette.View.extend({
ENDPOINTS: {},
$form: null,
submitted: false,
positiveReview: false,
ui: {
positiveReviewForm: '.simple-form.positive-review',
negativeReviewForm: '.simple-form.negative-review',
thumbsUp: '.thumbs-up',
thumbsDown: '.thumbs-down',
labelThumbsUp: '.label-thumbs-down',
labelThumbsDown: '.label-thumbs-up'
},
events: {
'click .thumbs-up': 'clickPositiveReview',
'click .thumbs-down': 'clickNegativeReview',
'click .skip-review': 'submitReviewSkip',
'click .btn-primary': 'submitReview'
},
initialize: function() {
this.ENDPOINTS = {
'create-event': this.$el.attr('data-id') + 'add-review/',
'feedback': this.$el.attr('data-id') + 'review/'
};
this.bindUIElements();
},
clickNegativeReview: function(event) {
this.positiveReview = false;
this.ui.positiveReviewForm.hide();
this.ui.thumbsDown.addClass("active");
this.ui.thumbsUp.removeClass("active");
this.ui.labelThumbsDown.show();
event.preventDefault();
this.ui.negativeReviewForm.show();
this.ui.form = this.ui.negativeReviewForm;
},
clickPositiveReview: function(event) {
this.positiveReview = true;
this.ui.negativeReviewForm.hide();
this.ui.thumbsUp.addClass("active");
this.ui.thumbsDown.removeClass("active");
this.ui.labelThumbsUp.show();
event.preventDefault();
this.ui.positiveReviewForm.show();
this.ui.form = this.ui.positiveReviewForm;
},
submitReviewSkip: function(event) {
this.ui.form.find('textarea').val('');
this.submitReview(event);
},
submitReview: function(event) {
event.preventDefault();
this.ui.form.hide();
if (this.submitted) {
return false;
}
this.submitted = true;
util.API.post(this.ENDPOINTS['create-event'], {
data: {
'is_helpful': this.positiveReview,
'text': this.ui.form.find('textarea').val()
},
csrf: true,
success: function() {
if (this.positiveReview) {
this.$el.removeClass()
.html('<div class="right-nav-box">' +
'<h3>Thanks for your feedback!</h3></div>');
} else {
location.href = this.ENDPOINTS.feedback;
}
}.bind(this)
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment