Skip to content
Browse files

Add frontend to the example rest user server that uses the connect mi…

…ddleware to validate data in the browser
  • Loading branch information...
1 parent b94dd01 commit fd4d2b80d313a1283ce12e2252c7bf5b56642047 @maritz committed Dec 7, 2011
View
2 examples/rest-user-server/UserModel.js
@@ -33,7 +33,7 @@ module.exports = nohm.model('User', {
validations: [
'notEmpty',
['length', {
- length: 4
+ min: 4
}]
]
},
View
40 examples/rest-user-server/client.js
@@ -0,0 +1,40 @@
+$(function () {
+ var updateUserList = function () {
+ $.get('/User/list', function (response) {
+ $ul = $('#users');
+ $ul.empty();
+ $.each(response, function (index, item) {
+ $ul.append('<li>'+JSON.stringify(item)+'</li>');
+ });
+ });
+ }
+ updateUserList();
+ $('#refreshusers').click(updateUserList);
+
+ $('form').submit(function (e) {
+ e.preventDefault();
+
+ var data = {};
+ $('input').each(function () {
+ var $this = $(this);
+ data[$this.attr('name')] = $this.val();
+ });
+ nohmValidations.validate('User', data, function (valid, errors) {
+ $('#errors').empty();
+ if (valid) {
+ $('form').attr('disabled', true);
+ $.post('/User/create', data, function (response) {
+ if (response.result === 'success') {
+ updateUserList();
+ } else {
+ $('#errors').append('<li>Server failure: '+response.data);
+ }
+ });
+ } else {
+ $.each(errors, function (index, error) {
+ $('#errors').append('<li>'+index+': '+JSON.stringify(error));
+ });
+ }
+ });
+ });
+});
View
21 examples/rest-user-server/index.html
@@ -0,0 +1,21 @@
+<html>
+<head>
+ <title>Nohm REST User server example</title>
+</head>
+<body>
+<a id="refreshusers">Refresh user list</a>
+ <ul id="users">
+ </ul>
+ <form novalidate="novalidate">
+ Errors: <ul id="errors"></ul><br/>
+ Name: <input type="text" name="name"/><br/>
+ Email: <input type="email" name="email"/><br/>
+ Password: <input type="password" name="password"/><br/>
+ <input type="submit" value="Submit"/>
+ </form>
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js" type="text/javascript"></script>
+ <script src="nohmValidations.js" type="text/javascript"></script>
+ <script src="client.js" type="text/javascript"></script>
+
+</body>
+</html>
View
15 examples/rest-user-server/rest-server.js
@@ -2,6 +2,7 @@ var express = require('express');
var Nohm = require(__dirname+'/../../lib/nohm.js').Nohm;
var UserModel = require(__dirname+'/UserModel.js');
var redis = require('redis');
+var fs = require('fs');
var redisClient = redis.createClient();
@@ -10,6 +11,8 @@ Nohm.setClient(redisClient);
var server = express.createServer();
+server.use(express.bodyParser());
+
server.get('/User/list', function (req, res, next) {
UserModel.find(function (err, ids) {
if (err) {
@@ -27,7 +30,7 @@ server.get('/User/list', function (req, res, next) {
if (err) {
return next(err);
}
- users.push({id: this.id, name: props.name});
+ users.push({id: this.id, name: props.name, email: props.email});
if (++count === len) {
res.json(users);
}
@@ -36,7 +39,7 @@ server.get('/User/list', function (req, res, next) {
});
});
-server.get('/User/create', function (req, res, next) {
+server.post('/User/create', function (req, res, next) {
var data = {
name: req.param('name'),
password: req.param('password'),
@@ -54,6 +57,14 @@ server.get('/User/create', function (req, res, next) {
});
});
+server.get('/', function (req, res) {
+ res.send(fs.readFileSync(__dirname+'/index.html', 'utf-8'));
+});
+
+server.get('/client.js', function (req, res) {
+ res.send(fs.readFileSync(__dirname+'/client.js', 'utf-8'), { 'Content-Type': 'text/javascript' });
+});
+
server.use(Nohm.connect([{
model: UserModel,
blacklist: ['salt']

0 comments on commit fd4d2b8

Please sign in to comment.
Something went wrong with that request. Please try again.