SyncProbe Class
Probe for exposing backbone data models from server-side persistence
This probe is used by the client-side Sync class to connect a local backbone model with server-side storage.
It delegates to a specialized SyncProbe defined by the server for the specific data class. For example, the server may determine that one class type uses FileSyncProbe, and another class uses a different persistence mechanism.
For security purposes, the server must configure specific SyncProbes for classes, or a default sync probe before this will operate.
Constructor
SyncProbe
-
className
-
[modelId]
-
[model]
Parameters:
-
className
StringName of the class to synchronize with
-
[modelId]
String optionalId of the data model for live synchronization If not set, a non-live probe is set up for control access only.
-
[model]
Object optionalIf this is a liveSync probe, this contains the attributes of the current model object.
Methods
create_control
-
model
-
callback
Create and save a new instance of the class into storage
This probe control requests a new instance of a data model to be
persisted onto storage. It is invoked when a data model that has
the Sync probe attached calls save()
on a new object.
Parameters:
delete_control
-
id
-
callback
Delete an instance from storage
This probe control deletes the instance with the specified id from storage.
Parameters:
-
id
StringID of the object to read
-
callback
Function(error)Callback when complete
-
error
MixedSet if an error occurs during read.
-
initialize
-
attributes
-
options
Initialize the probe
This is called on the probe during construction. It contains the probe initialization attributes and an option to make probe construction asynchronous.
Probe implementations can defer the initial response to the monitor until
the initial state is loaded. This allows the callback on
Monitor.connect()
to have the complete initial state of the probe when called.
If the initial probe state cannot be determined in initialize
, it should
set the options.asyncInit
option to true
, and call the
options.callback(error)
once the initial state is determined.
// Asynchronous initialization
options.asyncInit = true;
var callback = options.callback
If asyncInit
is set to true, the callback
must be called once
the initial state of the probe is known (or in an error condition).
// Set the initial state, and call the callback
this.set(...);
callback(null);
See the initialize
method of the FileProbe probe for an example. It defers
returning the probe to the monitor until the initial file contents are loaded.
Parameters:
-
attributes
ObjectInitial probe attributes sent in from the Monitor
-
options
ObjectInitialization options
-
asyncInit
BooleanSet this to TRUE if the initial probe state can't be known immediately.
-
callback
Function(error)The callback to call if asyncInit is set to true. If an error is passed, the probe will not be used.
-
onControl
-
name
-
[params]
-
[callback]
Dispatch a control message to the appropriate control function.
This is called when the
control()
method of a monitor is called.
The name determines the method name called on the probe.
The probe must implement a method with the name {name}_control()
,
and that method must accept two parameters - an input params and a callback.
The callback must be called, passing an optional error and response object.
For example, if the probe supports a control with the name go
, then
all it needs to do is implement the go_control()
method with the
proper signature. See ping_control()
for an example.
Parameters:
-
name
StringName of the control message.
-
[params]
Any optionalInput parameters specific to the control message.
-
[callback]
Function(error, response) optionalCalled to send the message (or error) response.
- error (Any) An object describing an error (null if no errors)
- response (Any) Response parameters specific to the control message.
ping_control
-
params
-
callback
Respond to a ping control sent from a monitor
Parameters:
-
params
ObjectInput parameters (not used)
-
callback
Function(error, response)Called to send the message (or error) response.
- error (Any) An object describing an error
- response (String) The string 'pong' is returned as the response
read_control
-
id
-
callback
Read an instance from storage
This probe control reads the instance with the specified id from storage, and returns it in the callback.
Parameters:
-
id
StringID of the object to read
-
callback
Function(error, result)Callback when complete
-
error
MixedSet if an error occurs during read. if error.code === 'NOTFOUND' then the requested object wasn't found. if error.code === 'PARSE' then the document was poorly formatted JSON.
-
result
ObjectThe full object.
-
release
()
Release any resources consumed by this probe.
This can be implemented by derived classes that need to be informed when they are to be shut down.
Probes that listen to events should use this method to remove their event listeners.
update_control
-
model
-
callback
Update a data model in storage
This acts like a REST PUT, meaning it can create a new object, or update an existing object.
Backbone has only a save() method. If the client sets the ID of the object before save(), Backbone thinks the object exists and will call update vs. create.
Properties
Config
<Object>
defaultProbe (String)
Name of the sync probe to use if the class isn't listed in the classMapclassMap (Object)
Map of className to sync probe name to use instead of the default for that class
Static Configurations
These can be set onto the Monitor.SyncProbe class after it's loaded.
The SyncProbe will not work until the defaultProbe is defined.
Example:
var syncConfig = Monitor.SyncProbe.Config;
syncConfig.defaultProbe = 'FileSyncProbe';
syncConfig.classMap = {
Book: 'MongoDbSync',
Author: 'MongoDbSync'
}