Skip to content

Instantly share code, notes, and snippets.

@ireneyiu
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ireneyiu/12da39007f64e58828f6 to your computer and use it in GitHub Desktop.
Save ireneyiu/12da39007f64e58828f6 to your computer and use it in GitHub Desktop.
var wfApp = wfApp || {};
wfApp.settings = wfApp.settings || {};
wfApp.settings.Password = function() {
this.old = '';
this.newPass = '';
this.verify = '';
};
wfApp.settings.Password.prototype.validate = function() {
if (!this.old.length) {
return {valid: false, errors: "Please enter your current password."};
} else if (this.newPass.length < 6) {
return {valid: false, errors: "New password must be at least 6 characters."};
} else if (this.newPass !== this.verify) {
return {valid: false, errors: "New password and confirmation do not match."};
} else if (this.newPass === this.old) {
return {valid: false, errors: "New password should be different from current password."};
} else {
return {valid: true};
}
};
wfApp.settings.PasswordView = function(opts) {
this.model = opts.model;
this.$el = $(opts.el);
this.$inputCurrent = this.$el.find(opts.inputCurrent);
this.$inputNew = this.$el.find(opts.inputNew);
this.submitUrl = this.$el.attr('action');
};
wfApp.settings.PasswordView.prototype.listen = function() {
this.$el.on('submit', this.submit.bind(this));
};
wfApp.settings.PasswordView.prototype.submit = function(e) {
e.preventDefault();
var params = this.$el.serializeArray();
this.model = $.extend(this.model, params);
var validationResult = this.model.validate();
if (!validationResult.valid) {
this.$inputNew.val("").first().focus();
this.showSubmitMessage(this.$el, validationResult.errors, "failure", 3000);
} else {
this.$el.find("input").attr("disabled", true);
this.showSubmitMessage(this.$el, "Saving...");
$.post(this.submitUrl, params)
.then(this.processResponse.bind(this))
.done(this.submitSuccess.bind(this))
.fail(this.submitFail.bind(this));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment