Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active December 18, 2015 03:19
Show Gist options
  • Save rjcorwin/5717215 to your computer and use it in GitHub Desktop.
Save rjcorwin/5717215 to your computer and use it in GitHub Desktop.
The bare minimum you must do to get Backbone.js Models to work with CouchDB.
var SomeModelClass = Backbone.Model.extend({
idAttribute: "_id",
url: function() {
if (_.has(this, 'id')) {
var url = (_.has(this.toJSON(), '_rev'))
? this.server + '/' + this.db + '/' + this.id + '?rev=' + this.get('_rev') // For UPDATE and DELETE
: this.server + '/' + this.db + '/' + this.id // For READ
}
else {
var url = this.server + '/' + this.db // for CREATE
}
return url
},
// Set your server. For example, http://127.0.0.1:5984
server: null,
// Set your database. In web browsers, to use the current database in browser's URL, use document.URL.split("/")[3]
db : null,
})
var someModel = new SomeModelClass({db: "someDatabase", server: "http://127.0.0.1:5984"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment