Skip to content

Instantly share code, notes, and snippets.

@ericf
Created August 18, 2011 05:02
Show Gist options
  • Select an option

  • Save ericf/1153325 to your computer and use it in GitHub Desktop.

Select an option

Save ericf/1153325 to your computer and use it in GitHub Desktop.
YUI 3 at: /Users/eferraiuolo/Tools/yui/yui3
YUI 3 Gallery at: /Users/eferraiuolo/Tools/yui/yui3-gallery
REST Model Sync Test Server running at: http://localhost:3000/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>REST Model Sync Manual Test</title>
</head>
<body>
<script src="/yui3?build/yui/yui.js"></script>
<script>
YUI({
comboBase : '/yui3?',
root : 'build/',
filter : (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
combine : true,
allowRollup : false,
modules : {
'gallery-model-sync-rest' : {
fullpath: '/yui3-gallery?build/gallery-model-sync-rest/gallery-model-sync-rest.js',
requires: ['io-base', 'json-stringify']
}
}
}).use('model', 'model-list', 'gallery-model-sync-rest', function (Y) {
var User, Users, user;
User = Y.Base.create('user', Y.Model, [Y.ModelSync.Rest], {
root : '/user'
}, {
ATTRS : {
name : {}
}
});
Users = Y.Base.create('users', Y.ModelList, [Y.ModelSync.Rest], {
url : '/user',
model : User
});
user = new User({ name: 'Eric' });
user.save(function () {
Y.log(user.get('id'));
});
});
</script>
</body>
</html>
var fs = require('fs'),
combo = require('combohandler'),
express = require('express'),
app = express.createServer(),
users = [],
yui3Path = __dirname + '/../../../../../yui3',
yui3GalleryPath = __dirname + '/../../../../';
app.configure(function(){
// Handles parsing HTTP request entitiy bodies from the client.
app.use(express.bodyParser());
});
app.get('/', function (req, res) {
res.sendfile('model-sync-rest.html');
});
// YUI 3 combo handler.
app.get('/yui3', combo.combine({ rootPath: yui3Path }), function (req, res) {
res.send(res.body, 200);
});
// YUI 3 Gallery combo handler.
app.get('/yui3-gallery', combo.combine({ rootPath: yui3GalleryPath }), function (req, res) {
res.send(res.body, 200);
});
app.get('/user', function (req, res) {
res.json(users);
});
app.post('/user', function (req, res) {
var user = req.body;
user.id = users.length;
users.push(user);
res.json(user);
});
// Get the user, set it on the request object, and continue or error out.
app.all('/user/:id', function (req, res, next) {
var id = req.params.id,
user = users[id];
if (user) {
req.user = user;
next();
} else {
res.send('Cannot find user: ' + id, 404);
}
});
app.get('/user/:id', function (req, res) {
res.json(req.user);
});
app.put('/user/:id', function (req, res) {
users[req.user.id] = req.body;
res.send();
});
app.del('/user/:id', function (req, res) {
users.splice(req.user.id, 1);
res.send();
});
// Toss a 404 for everything else.
app.get('*', function (req, res) {
res.send('REST Model Sync Test Server', 404);
});
// Go Go Gadget Server.
app.listen(3000);
// Say what's up with the env.
console.log('YUI 3 at: ' + fs.realpathSync(yui3Path));
console.log('YUI 3 Gallery at: ' + fs.realpathSync(yui3GalleryPath));
console.log('REST Model Sync Test Server running at: http://localhost:3000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment