0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00
ghost/core/client/views/login.js
2013-08-01 15:28:13 +01:00

119 lines
3.3 KiB
JavaScript

/*global window, document, Ghost, $, _, Backbone, JST */
(function () {
"use strict";
Ghost.Views.Login = Ghost.View.extend({
templateName: "login",
events: {
'submit #login': 'submitHandler'
},
initialize: function (options) {
this.render();
$(window).trigger('resize');
},
template: function (data) {
return JST[this.templateName](data);
},
render: function () {
var self = this;
this.$el.html(this.template());
$(window).on('resize', _.debounce(function (e) {
$(".js-login-container").center();
}, 100));
$(window).one('centered', self.fadeInAndFocus);
return this;
},
submitHandler: function (event) {
event.preventDefault();
var email = this.$el.find('.email').val(),
password = this.$el.find('.password').val(),
self = this;
$.ajax({
url: '/ghost/login/',
type: 'POST',
data: {
email: email,
password: password
},
success: function (msg) {
window.location.href = msg.redirect;
},
error: function (obj, string, status) {
self.addSubview(new Ghost.Views.NotificationCollection({
model: [{
type: 'error',
message: 'Invalid username or password',
status: 'passive'
}]
}));
}
});
},
fadeInAndFocus: function () {
$(".js-login-container").fadeIn(750, function () {
$("[name='email']").focus();
});
}
});
Ghost.Views.Signup = Ghost.View.extend({
templateName: "signup",
events: {
'submit #register': 'submitHandler'
},
initialize: function (options) {
this.render();
},
template: function (data) {
return JST[this.templateName](data);
},
render: function () {
this.$el.html(this.template());
return this;
},
submitHandler: function (event) {
event.preventDefault();
var email = this.$el.find('.email').val(),
password = this.$el.find('.password').val(),
self = this;
$.ajax({
url: '/ghost/signup/',
type: 'POST',
data: {
email: email,
password: password
},
success: function (msg) {
window.location.href = msg.redirect;
},
error: function (obj, string, status) {
var msgobj = $.parseJSON(obj.responseText);
self.addSubview(new Ghost.Views.NotificationCollection({
model: [{
type: 'error',
message: msgobj.message,
status: 'passive'
}]
}));
}
});
}
});
}());