Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2013 20:19
Show Gist options
  • Save anonymous/4455624 to your computer and use it in GitHub Desktop.
Save anonymous/4455624 to your computer and use it in GitHub Desktop.
Look for the bindEvents function to see a simpler version of backbone events. Require jquery.
Selfstarter = window.Selfstarter = {
firstTime: true,
value: null,
form: $('#order_form'),
valueField: $('#order_form').find('input[name="order[value]"]'),
email: $('#order_email'),
button: $('#self_button'),
step1: $('.step_1'),
step2: $('.step_2'),
step3: $('.step_3'),
links: $('ol.values li a'),
initialize: function() {
this.bindEvents({
'ol.values li a click' : 'showPaymentOptions',
'ol.payment_options li a click' : 'showForm'
});
this.validateEmail();
},
showForm: function(event,target) {
this.step2.addClass('grayscale');
this.step3.fadeIn();
},
showPaymentOptions: function(event, target){
this.links.removeClass('selected');
target.addClass('selected');
this.valueField.val(target.data('value'));
this.step1.addClass('grayscale');
this.step2.fadeIn();
},
isValidEmail: function(email) {
return /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/.test(email)
},
validateEmail: function(){
// It probably should be a parser, but there isn't enough time for that (Maybe in the future though!)
//
if ( this.isValidEmail(this.email.val()) ) {
this.email.removeClass('highlight');
this.button.removeClass('disabled');
}
else {
if ( this.firstTime ) {
this.email.addClass('highlight');
}
if ( !this.button.hasClass('disabled') ) {
this.button.addClass("disabled");
}
}
},
// Binding multiple events in an Object hash, avoiding repetition
// Syntax:
//
// Selfstarter.bindEvents({
// 'div.example mouseover' : 'showSomeContent'
// });
//
// showSomeContent: function() { alert("I'm calling a function!"); }
//
bindEvents: function(events) {
for(evt in events) {
(function(evt) {
var options = evt.split(' ');
var bindEvent = options.pop();
var target = $(options.join(' '));
var fn = Selfstarter[events[evt]];
target.on(bindEvent, function(event) { fn.apply(Selfstarter,[event, jQuery(event.target)]) })
})(evt);
}
},
};
(Selfstarter.initialize)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment