Node Monitor v0.6.5

Show:

Sync Class

Defined in: lib/Sync.js:91
Module: Monitor

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.

Methods

_connectClassMonitor

(
  • method
  • model
  • [options]
  • callback
)
private

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 String

    The requested CRUD method

  • model Backbone.Model

    The data model to perform the operation on

  • [options] Object optional

    Options

    • [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
)
private

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.

Parameters:

  • method String

    The requested CRUD method

  • model Backbone.Model

    The data model to perform the operation on

  • callback Function(error, params)
    • Called when connected
    • error Mixed
      • Set if it couldn't connect
    • params Object
      • Updated data model parameters

_getMonitorParams

(
  • [modelId]
)
Object private

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 optional

    Id 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:

Object: The monitor parameters

_getOpts

(
  • method
  • model
)
Object private

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 Enum

    One of the CRUD methods

  • model Backbone.Model

    The model to prepare the opts from

Returns:

Object: The options object to pass to the probe

_sync

(
  • method
  • model
  • [options]
)
private

Provide the sync API to a backbone data model

See the Backbone documentation for more information on this method.

Parameters:

  • method String

    A CRUD enumeration of "create", "read", "update", or "delete"

  • model Backbone.Model or Backbone.Collection

    The model or collection to act upon

  • [options] Object optional

    Success 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]
)
Sync static

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.

Parameters:

  • className String

    Name of the class to synchronize with

  • [options] Object optional

    Additional sync options

    • hostName String

      Host name to use for the Sync probe. If not specified, the closest server hosting Sync probe will be determined (this server, or the default gateway)

    • appName String

      Server appName (see Monitor.appName)

    • appInstance String

      Application instance (see Monitor.appInstance)

Returns:

Sync: A sync method to assign to a Backbone class or instance.