-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.html
127 lines (114 loc) · 4.81 KB
/
jquery.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>MyPets</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="example.css" />
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/demock/demock.js"></script>
<script src="bower_components/demock-jquery/demock-jquery.js"></script>
<script>
demock.init($);
$(function () {
// @todo determine .abort() behaviour with or without delay
$('#signin').submit(function (evt) {
$('#error, #http-status').addClass('hidden');
var credentials = {
username: $('#username').val(),
password: $('#password').val()
};
$('#signin')
.addClass('-pending')
.find('input, button').attr('disabled', true);
$.ajax({ type: 'POST', url: 'api/signin', data: credentials })
.done(function (data, textStatus, jqXHR) {
$('#signin, #http-status').addClass('hidden');
$('#signout, #welcome').removeClass('hidden');
$.ajax({ url: 'api/userInfo' })
.done(function (userInfo) {
$('#identity')
.removeClass('hidden')
.text(userInfo.firstName + ' ' + userInfo.lastName);
});
})
.fail(function (jqXHR, textStatus, errorThrown) {
var reason;
switch (textStatus) {
case 'error':
try {
reason = JSON.parse(jqXHR.responseText).error;
} catch (e) {
reason = 'Error';
}
break;
case 'timeout':
reason = 'Connection timeout';
break;
}
if (errorThrown && errorThrown != jqXHR.statusText) {
reason += ' (' + errorThrown + ')';
}
$('#error')
.removeClass('hidden')
.text('Authentication failed: ' + reason);
if (jqXHR.status) {
$('#http-status')
.removeClass('hidden')
.text('HTTP ' + jqXHR.status + ' ' + jqXHR.statusText);
}
})
.always(function () {
$('#signin')
.removeClass('-pending')
.find('input, button').removeAttr('disabled');
});
evt.preventDefault();
});
$('#signout').submit(function (evt) {
$('#signin, #http-status').removeClass('hidden');
$('#signout, #welcome, #identity').addClass('hidden');
evt.preventDefault();
});
});
</script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">MyPets</a>
</div>
<form id="signout" class="navbar-form navbar-right hidden">
<button class="btn btn-default">Sign Out</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#" id="identity" class="hidden"></a></li>
</ul>
</nav>
<div class="row">
<div class="col-xs-4 col-xs-offset-4">
<form id="signin" class="well well-lg">
<div id="error" class="alert alert-danger hidden"></div>
<div class="form-group">
<label for="username">Username</label>
<input id="username" name="username" value="john" class="form-control" />
<p class="help-block">Also try: <strong>delay</strong>, <strong>timeout</strong></p>
</div>
<div class="form-group">
<label for="password">Password</label>
<input id="password" type="password" value="password" class="form-control" />
<p class="help-block">
The correct password for username <strong>john</strong> is <strong>password</strong>
</p>
</div>
<button class="btn btn-primary btn-lg btn-block">Sign In</button>
</form>
<p id="http-status" class="text-muted"></p>
<div id="welcome" class="alert alert-success hidden">
<strong>Welcome!</strong> You have successfully signed in.
</div>
</div>
</div>
</body>
</html>