Up tonight, I hope to get multiple updates working with my Backbone.js calendar application when using faye as the persistence layer.
When I make changes, I am sending the update as a message from my Backbone application to the
/calendars/update faye channel. The updated information is sent back from the server on the /calendars/changes faye channel:The problem is that I am not doing anything with that change information. Ordinarily that would not be a problem, but my backend storage is CouchDB. CouchDB really needs that
rev attribute. If a subsequent update is sent with the old rev, CouchDB's optimistic locking will kick in and reject the update:Got response: 409 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ error: 'conflict',
reason: 'Document update conflict.' }So, in my client, I subscribe to the /calendars/changes channel. In the subscription's callback, I use the revision published by the server to update the Backbone model: faye.subscribe('/calendars/changes', function(message) {
console.log('[/calendars/changes]');
console.log(message);
var model = calendar.appointments.get(message);
model.set({rev: message.rev});
model.set({_rev: message.rev});
});With, that, I can update an appointment. And update it again... and it works:Got response: 201 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ ok: true,
id: 'cd823d4aaaf358069f9a800410000b95',
rev: '7-55b3b95b6996072aa1519b6070cf12b6' }
Got response: 201 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ ok: true,
id: 'cd823d4aaaf358069f9a800410000b95',
rev: '8-a06160ea8bef50bd71d92a73db61c14b' }Yay! Except... immediately after I see the successful update, I see:Got response: 201 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ ok: true,
id: 'cd823d4aaaf358069f9a800410000b95',
rev: '8-a06160ea8bef50bd71d92a73db61c14b' }
Got response: 409 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ error: 'conflict',
reason: 'Document update conflict.' }When I update again, I successfully update the record, which is followed immediately by two conflicts:Got response: 201 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ ok: true,
id: 'cd823d4aaaf358069f9a800410000b95',
rev: '9-c09e4a0a8bbd5cc25a9e67b19b6a254b' }
Got response: 409 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ error: 'conflict',
reason: 'Document update conflict.' }
Got response: 409 localhost:5984/calendar/cd823d4aaaf358069f9a800410000b95
{ error: 'conflict',
reason: 'Document update conflict.' }Ah. I know what that is. And I will fix it. But first, I need to finish proof reading the recipes that taught me how to solve it. Recipes with Backbone. Going alpha tonight.
Day #149

No comments:
Post a Comment