Last active
December 18, 2015 03:19
-
-
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.
This file contains hidden or 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 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