Last active
December 21, 2015 22:19
-
-
Save nnarhinen/6374611 to your computer and use it in GitHub Desktop.
Automatically keep your backbone models up-to-date via web sockets by listening to couchdb changes feed
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 db = new (require('cradle').Connection)().database('my_db'); | |
db.info(function(err, result) { | |
var seq = result.update_seq; | |
db.changes({since: seq, include_docs: true}).on('change', function(change) { | |
if (change.doc && change.doc.owner) { | |
io.sockets.in(change.doc.owner).emit('change:' + change.doc.type, decorate(change.doc)); | |
} | |
}); | |
}); |
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 SomeModel = Backbone.Model.extend({ | |
initialize: function() { | |
mySocket.on('change:some_type', function(data) { | |
if (data.id != this.id) return; | |
this.attributes = data; | |
this.trigger('change'); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment