Created
August 24, 2014 21:47
-
-
Save Siedrix/adbb7a3277fce7abaff4 to your computer and use it in GitHub Desktop.
Backbone urls
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
{ | |
"name": "url-patterns", | |
"version": "0.0.0", | |
"authors": [ | |
"Siedrix <[email protected]>" | |
], | |
"license": "MIT", | |
"ignore": [ | |
"**/.*", | |
"node_modules", | |
"bower_components", | |
"test", | |
"tests" | |
], | |
"dependencies": { | |
"backbone": "~1.1.2" | |
} | |
} |
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
<html> | |
<head> | |
<title>Url</title> | |
</head> | |
<body> | |
<script type="text/javascript" src="/vendors/jquery/dist/jquery.min.js "></script> | |
<script type="text/javascript" src="/vendors/underscore/underscore.js"></script> | |
<script type="text/javascript" src="/vendors/backbone/backbone.js"></script> | |
<script type="text/javascript"> | |
window.Item = Backbone.Model.extend({ | |
idAttribute : '_id', | |
urlRoot : '/api/v1/items' | |
}); | |
window.Items = Backbone.Collection.extend({ | |
model : window.Item, | |
url : '/api/v1/items' | |
}); | |
window.items = new window.Items(); | |
var xhr = window.items.fetch(); | |
xhr.done(function () { | |
// Update | |
var f = window.items.first(); | |
f.set('value', 'moo'); | |
f.save(); | |
// Create | |
var m = window.items.add({name:'quz', type:'variable'}); | |
m.save(); | |
// Delete | |
var s = window.items.get(325); | |
s.destroy(); | |
}); | |
window.item = new window.Item({_id:324}); | |
window.item.fetch(); | |
</script> | |
</body> | |
</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
{ | |
"name": "url-patterns", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "BSD-2-Clause", | |
"dependencies": { | |
"express": "~4.8.5", | |
"body-parser": "~1.6.5", | |
"underscore": "~1.6.0" | |
} | |
} |
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
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var _ = require('underscore'); | |
var app = express(); | |
app.use(bodyParser.json()); | |
app.use('/vendors', express.static(__dirname + '/bower_components')); | |
app.use(function (req, res, next) { | |
console.log(req.method, req.path); | |
if( !_.isEmpty(req.body) ){ | |
console.log(req.body); | |
} | |
next(); | |
}); | |
app.get('/', function (req, res) { | |
res.sendFile( __dirname + '/index.html'); | |
}); | |
// COLLECTION METHODS | |
// On collection fetch | |
app.get('/api/v1/items', function (req, res) { | |
res.send([ | |
{_id : 324, name:'foo', type:'variable'}, | |
{_id : 325, name:'bar', type:'variable'}, | |
{_id : 326, name:'LOW', type:'constant'}, | |
{_id : 327, name:'RED', type:'constant'} | |
]); | |
}); | |
// MODELS METHODS | |
// On model.create | |
app.post('/api/v1/items/', function (req, res) { | |
res.send({_id:328}); | |
}); | |
// On model.fetch | |
app.get('/api/v1/items/:id', function (req, res) { | |
res.send({_id:328}); | |
}); | |
// On model.save | |
app.put('/api/v1/items/:id', function (req, res) { | |
res.send({status:'ok'}); | |
}); | |
// On model.delete | |
app.del('/api/v1/items/:id', function (req, res) { | |
res.send({status:'ok'}); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a sample of the urls that a backbone app needs on the server.