Created
May 23, 2014 13:16
-
-
Save rjcorwin/4ddef7fd6c66729063b0 to your computer and use it in GitHub Desktop.
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 db = new PouchDB('http://localhost:5984/dbname'); | |
| var Thing = Backbone.Model.extend({ | |
| idAttribute: "_id", | |
| initialize: function() { | |
| var model = this | |
| var changes = db.changes({ | |
| live: true, | |
| onChange: function(change) { | |
| if(change.id == model.id) { | |
| db.get(change.id, function(err, doc) { | |
| model.set(doc) | |
| }) | |
| } | |
| } | |
| }) | |
| }, | |
| sync: function(method, model, options) { | |
| switch (method) { | |
| case 'read': | |
| var id = (model.get('_id')) ? model.get('_id') : model.id | |
| db.get(id, function(err, doc) { | |
| if (err) return console.log(err) | |
| options.success(doc) | |
| }) | |
| break | |
| case 'update': | |
| var id = (model.get('_id')) ? model.get('_id') : model.id | |
| db.put(model.toJSON(), function(err, response) { | |
| console.log(response) | |
| }) | |
| break | |
| } | |
| } | |
| }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment