Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend UI to require user name input
This includes the addition of a "User" model and associated unit tests.
- Loading branch information
1 parent
adfd2dd
commit c925664
Showing
9 changed files
with
129 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
define([ | ||
'text!templates/login.html', 'layoutmanager', '_' | ||
], function(html, Backbone, _) { | ||
'use strict'; | ||
|
||
var LoginView = Backbone.Layout.extend({ | ||
className: 'login', | ||
events: { | ||
submit: 'setName' | ||
}, | ||
template: _.template(html), | ||
setName: function(event) { | ||
var result = this.model.set('name', this.$('.username').val(), | ||
{ validate: true }); | ||
if (result) { | ||
this.isPending = true; | ||
} | ||
this.render(); | ||
event.preventDefault(); | ||
}, | ||
serialize: function() { | ||
return { | ||
user: this.model.toJSON(), | ||
login: { | ||
error: this.model.validationError, | ||
isPending: this.isPending | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
return LoginView; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
define(['backbone'], function(Backbone) { | ||
'use strict'; | ||
|
||
var User = Backbone.Model.extend({ | ||
nameRegex: /^[0-9a-z\.-]+$/i, | ||
validate: function(attrs) { | ||
if (!attrs || !attrs.name) { | ||
return new Error('No username specified'); | ||
} else if (!this.nameRegex.test(attrs.name)) { | ||
return new Error('Invalid username'); | ||
} | ||
} | ||
}); | ||
|
||
return User; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="screen"></div> | ||
<div class="dialog"> | ||
<h1 class="title">Please Log In</h1> | ||
<% if (login.error) { %> | ||
<div class="error"><%= login.error %></div> | ||
<% } %> | ||
<form> | ||
<input type="text" class="username"<%= login.isPending ? ' disabled' : '' %>> | ||
<input type="submit" class="submit" value="Submit"<%= login.isPending ? ' disabled' : '' %>> | ||
</form> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
define([ | ||
'tests/user' | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
define(['modules/user'], function(User) { | ||
'use strict'; | ||
|
||
suite('User', function() { | ||
suite('#validate', function() { | ||
var user; | ||
suiteSetup(function() { | ||
user = new User(); | ||
}); | ||
test('Rejects unspecified string names', function() { | ||
assert.instanceOf(user.validate(), Error); | ||
assert.instanceOf(user.validate({ name: '' }), Error); | ||
}); | ||
test('Rejects invalid names', function() { | ||
assert.instanceOf(user.validate({ name: '!@#@#$' }), Error); | ||
}); | ||
}); | ||
}); | ||
}); |