Skip to content

Instantly share code, notes, and snippets.

@zuk
Created June 5, 2012 19:06
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zuk/2877040 to your computer and use it in GitHub Desktop.
DrowsyDromedary usage with Backbone.js
var drowsyUrl = 'http://localhost:9292';
var db = 'fridge';
/** Set up a basic Model class for Drowsy that we will extend from **/
var DrowsyModel = Backbone.Model.extend({
idAttribute: '_id',
// needed to deal with the way Drowsy returns ObjectIds
parse: function(data) {
data._id = data._id.$oid;
return data;
}
});
/** A class representing a single vegetable **/
var Vegetable = DrowsyModel.extend({
urlRoot: drowsyUrl + '/' + db + '/vegetables'
});
/** A class representing a collection of vegetables **/
var Vegetables = Backbone.Collection.extend({
model: Vegetable,
url: drowsyUrl + '/' + db + '/vegetables'
});
/** Okay we're ready to start using it... **/
// instantiate a new vegetable
var veg = new Vegetable({type: "broccoli", colour: "green", quantity: 4});
// now submit it to the database
veg.save();
// or if we want confirmation
veg.save({success: function () { console.log("Saved!"); }});
// retrieve all green vegetables, sorted by quantity in descending order
var greenVeggies = new Vegetables();
greenVeggies.fetch({
data: {
selector: JSON.stringify({colour: "green"}),
sort: JSON.stringify(['quantity', 'DESC'])
}
});
// now get the first vegetable in the collection we retrieved
var veg = greenVeggies.first();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment