Backbone.js: cha cha changes - keep your client in sync

In rich client javascript applications it is often desirable to keep your client side representations up-to-date with any changes that happen on the server. This post describes a strategy for doing so when using Backbone.js.

We could have every model take the responsibility for polling the server itself, but that would typically mean an awful lot of unnecessary requests. To reduce the number of requests, we can have the Collection do the polling. Yes, we lose being able to us HTTP's conditional GET, but sometimes we have to eschew high ideals in the real world!

Thanks to this pull request being incorporated into backbonejs, doing this just got a little easier. Now we can provide a merge option to Collection.fetch() which will merge rather than discard duplicates. Note you also have to provide the add option or the reset would naturally be called removing any existing models from the collection. I'm also assuming all models have a lastUpdated attribute. Such an attribute is quite common for domain classes on server side frameworks like Grails and Rails.

Comments

Post a Comment

Popular posts from this blog

AngularJs: User friendly date display with AngularJs and MomentJs

Nerd Tree: A File Explorer with Mac Vim

Getting started with Grails functional tests using Geb + Spock