July 11th, 2013, 04:47 PM
Backbone.js - getting model to detect when changes have occurred to the DB state
I'm just getting started learning about how Backbone.js may be beneficial.
I've got my head around the basic MVC principles as implemented by Backbone.js.
What's confusing me, is that what I want to achieve is a way by which the Backbone.js model can detect changes to the state of its associated entity in the DB and then update its model accordingly and, thus any view listening on that model.
Appreciate your thoughts on how this can be realised using backbone.js, given this seems like a fairly fundamental and common requirement?
I should clarify that I don't wish for the JS library to talk directly to the DB, but via RESTFul interfaces which in turn talk through the typical n-tiers of service, business & persistence tiers.
July 11th, 2013, 08:15 PM
A system like that is actually quite a bit beyond the scope of libraries like backbone.js, not because it's not useful, but because it's complicated and requires specific server-side code and data structures to support it.
On the client-side, the most universal way of doing this is to use a polling loop to execute an HTTP request every X seconds and see whether there are any changes on the server-side.
On the server-side, you have to track the changes to the database in some way. There are a lot of ways to do this, but in a nutshell whatever method you choose needs to be able to efficiently determine when and what changes were made to the database.
In your polling loop, the server checks for any changes and returns those back to the client-side for processing (like updating your backbone models).
This sort of system tends to be fairly costly from a performance perspective.