Sync Class
Live data model synchronization.
This class can be attached to Backbone models to synchronize backend data using the
fetch
,
save
, and
destroy
Backbone API methods.
It also provides two-way change based synchronization, updating data on the server as changes are made to the model, and updating the client model as changes are detected on the server.
Communication is Probe based, leveraging the built-in connection, routing, and socket-io functionality. The FileSyncProbe is provided for file-based model persistence, and others can be written to implement alternate persistence mechanisms.
Item Index
Methods
Methods
_connectClassMonitor
-
method
-
model
-
[options]
-
callback
Connect and send the control message to a Sync probe for this class.
This creates a monitor to a Sync probe with the specified className. The monitor is used to send CRUD control messages for any ID within the class.
Once connected, it sends the specified control message to the probe.
This monitor is used for non-liveSync interactions.
Parameters:
-
method
StringThe requested CRUD method
-
model
Backbone.ModelThe data model to perform the operation on
-
[options]
Object optionalOptions
-
[silenceErrors]
optional- Silence the logging of errors (they're expected)
-
-
callback
Function(error, params)- Called when connected
-
error
Mixed- Set if it couldn't connect
-
params
Object- Updated data model parameters
_connectInstanceMonitor
-
method
-
model
-
callback
Connect and send the control message to a liveSync monitor for the model
This creates a monitor to a Sync probe for the model instance, and attaches event listeners onto the monitor and the data model.
Once connected, it sends the specified control message to the probe.
Changes on the server are automatically propagated to the local data model, and local changes to the data model are automatically propagated to the server.
_getMonitorParams
-
[modelId]
Prepare the init parameters for a monitor to a Sync probe
The monitor init params for the class monitor and the liveSync model monitor only differ in the modelId, so this method was broken out to reduce code duplication.
Parameters:
-
[modelId]
String optionalId to the data model. If set, then params will be built for liveSync to a data model with that id. params for the class.
Returns:
_getOpts
-
method
-
model
Prepare the control options
This prepares the control options to include the ID element on a fetch or delete, and the entire model on a create or update.
Parameters:
-
method
EnumOne of the CRUD methods
-
model
Backbone.ModelThe model to prepare the opts from
Returns:
_sync
-
method
-
model
-
[options]
Provide the sync API to a backbone data model
See the Backbone documentation for more information on this method.
Parameters:
-
method
StringA CRUD enumeration of "create", "read", "update", or "delete"
-
model
Backbone.Model or Backbone.CollectionThe model or collection to act upon
-
[options]
Object optionalSuccess and error callbacks, and additional options to pass on to the sync implementation.
-
[liveSync]
optional- Turn on the live update functionality
-
[silenceErrors]
optional- Silence the logging of errors (they're expected)
-
[success]
optional- The method to call on method success
-
[error]
optional- The method to call on method error
-
Sync
-
className
-
[options]
Probe based data synchronization with server-side storage.
This method returns a function conforming to the Backbone
Sync
API, offering
fetch
,
save
, and
destroy
functionality to any Backbone data model.
The returned function can be assigned to the sync
element when defining the
data model:
var BlogEntry = Backbone.Model.extend({
...
sync: Monitor.Sync('BlogEntry'),
...
});
The sync function can also be assigned to any Backbone model after construction:
var myBook = new Book({id:"44329"});
myBook.sync = Monitor.Sync('Book');
myBook.fetch();
In addition to providing the standard fetch
, save
, and destroy
functionality, Sync offers live data synchronization, updating the data model
as changes are detected on the server.
// Turn on live data synchronization
myBook.fetch({liveSync:true});
This fetches the myBook
instance with the contents of the Book class
id 44329
, persists local changes to myBook
, and keeps myBook
up to date with changes detected on the server.
Live data synchronization consumes resources on both the client and server.
To release those resources, make sure to call the clear()
method on
the data model. Otherwise, resources are released when the server connection
is terminated.
// Clear the object, turning off live synchronization
myBook.clear();
See the Backbone documentation for more information about the Backbone.sync functionality.