Slide 27
Slide 27 text
function AjaxForm($el) {
this.$form = $el;
this.form = $el[0];
this.$form.on('submit', $.proxy(this.submit, this));
};
AjaxForm.prototype = {
submit: function(e) {
e.preventDefault();
var _this = this;
$.ajax({
url: _this.form.action,
type: _this.form.method,
success: $.proxy(this.onSuccess, _this)
});
},
onSuccess: function(data) {
$({}).trigger('save.success', data);
this.reset();
},
reset: function() {
this.form.reset();
}
};
Loose coupling
Single responsibility principle
Named functions
Internal state via properties