-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 80342ad
Showing
46 changed files
with
52,453 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
*.swp | ||
*.swo | ||
node_modules | ||
_SpecRunner.html |
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,53 @@ | ||
{ | ||
"predef": [ | ||
"sinon", | ||
"before", | ||
"after", | ||
"it", | ||
"xit", | ||
"describe", | ||
"expect", | ||
"beforeEach", | ||
"afterEach", | ||
"spyOn", | ||
"waits", | ||
"waitsFor", | ||
"runs", | ||
"_", | ||
"sba", | ||
"Backbone", | ||
"jasmine", | ||
"afterEach" | ||
], | ||
|
||
"node" : true, | ||
"browser" : true, | ||
"jquery" : true, | ||
"boss" : false, | ||
"curly": true, | ||
"debug": false, | ||
"devel": false, | ||
"eqeqeq": true, | ||
"evil": false, | ||
"forin": false, | ||
"immed": true, | ||
"laxbreak": false, | ||
"newcap": true, | ||
"noarg": true, | ||
"noempty": false, | ||
"nonew": true, | ||
"onevar": false, | ||
"plusplus": false, | ||
"multistr": true, | ||
"regexp": false, | ||
"undef": true, | ||
"sub": false, | ||
"strict": false, | ||
"bitwise" : true, | ||
"curly" : true, | ||
"latedef" : false, | ||
"noarg" : true, | ||
"trailing" : true, | ||
"asi" : false, | ||
"expr": true | ||
} |
Empty file.
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,104 @@ | ||
// Implements an in-memory server for Backbone.js | ||
// http://documentcloud.github.com/backbone/#Sync | ||
|
||
var express = require('express'), | ||
app = express(), | ||
idCounter = 0, | ||
data = {}, | ||
port = 3002, | ||
_ = require('underscore'); | ||
|
||
app.configure(function () { | ||
app.use(express.bodyParser()); | ||
app.use(allowCrossDomain); | ||
app.use(express.static(__dirname + '/public')); | ||
}); | ||
|
||
app.get('/', function(req, res) { | ||
res.sendfile('public/index.html'); | ||
}); | ||
|
||
app.get('/tests', function(req, res) { | ||
res.sendfile('public/tests.html'); | ||
}); | ||
|
||
app.get("/:collection", function(req, res) { | ||
console.log('read ' + req.params.collection); | ||
if (!data[req.params.collection]) { | ||
return res.send(404); | ||
} | ||
|
||
data[req.params.collection] = data[req.params.collection] || []; | ||
res.send(data[req.params.collection]); | ||
}); | ||
|
||
// create -> POST /collection | ||
app.post('/:collection', function(req, res){ | ||
console.log('create ' + req.params.collection); | ||
console.log(req.body); | ||
req.body.id = idCounter++; | ||
data[req.params.collection] = data[req.params.collection] || []; | ||
data[req.params.collection].push(req.body); | ||
res.send({ id: req.body.id }); | ||
}); | ||
|
||
// read -> GET /collection[/id] | ||
app.get('/:collection/:id?', function (req,res) { | ||
console.log('read ' + (req.params.id || ('collection ' + req.params.collection))); | ||
|
||
if (data[req.params.collection] && !req.params.id) { | ||
return res.send(data[req.params.collection]); | ||
} | ||
|
||
var model = findModel(req.params.collection, req.params.id); | ||
if (!model && req.params.id) { | ||
return res.send('cant find model ' + req.params.id, 404); | ||
} | ||
res.send(model); | ||
}); | ||
|
||
// update -> PUT /collection/id | ||
app.put('/:collection/:id', function (req,res) { | ||
console.log('update ' + req.params.collection + ' - ' + req.params.id); | ||
removeModel(req.params.collection, req.params.id); | ||
data[req.params.collection].push(req.body); | ||
res.send(200); | ||
}); | ||
|
||
// delete -> DELETE /collection/id | ||
app.delete('/:collection/:id', function (req,res) { | ||
console.log('delete ' + req.params.collection + ' - ' + req.params.id); | ||
removeModel(req.params.collection, req.params.id); | ||
res.send(200); | ||
}); | ||
|
||
app.listen(port); | ||
console.log('Backbone-server started at port %d.', port); | ||
|
||
function removeModel(collection, id) { | ||
if (!data[collection]) { | ||
console.log('cant find collection ' + collection); | ||
return; | ||
} | ||
data[collection] = _(data[collection]).reject(function (model) { | ||
return model.id === parseInt(id, 10); | ||
}); | ||
} | ||
|
||
function findModel(collection, id) { | ||
if (!data[collection]) { | ||
console.log('cant find collection ' + collection); | ||
return; | ||
} | ||
return _(data[collection]).find(function (m) { | ||
return m.id === parseInt(id, 10); | ||
}); | ||
} | ||
|
||
function allowCrossDomain(req, res, next) { | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); | ||
res.header('Access-Control-Allow-Headers', 'Content-Type'); | ||
|
||
next(); | ||
} |
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,12 @@ | ||
{ | ||
"name": "application-name", | ||
"version": "0.0.1", | ||
"private": true, | ||
"scripts": { | ||
"start": "node app" | ||
}, | ||
"dependencies": { | ||
"express": "3.0.0rc4", | ||
"jade": "*" | ||
} | ||
} |
Oops, something went wrong.