channel.js |
|
'use strict';
|
|
¶ Telepat Channel ClassUse Channels to create, update and remove Telepat objects. You can create new Channels using the |
var EventObject = require('./event');
|
¶ Channel ConstructorThis is generally invoked by the main Telepat object, when calling the Params
api
Object
The API connection object. This is injected by Telepat.
log
Object
The logging-handling object. This is injected by Telepat.
error
Object
The error-handling object. This is injected by Telepat.
options
Object
The object describing the required subscription (context, channel, filters)
interval
integer
The time interval in miliseconds between two object-monitoring jobs. Defaults to 150.
|
var Channel = function (tapi, tlog, terror, tmonitor, toptions) {
var api = tapi;
var error = terror;
var monitor = tmonitor;
var options = toptions;
var log = tlog;
var event = new EventObject(log);
var self = this;
|
¶ Channel.objectsYou can access a hash of all the objects on the current channel using this property. Each object is stored on a key named after the object id. |
this.objects = {};
|
¶ Channel.subscribeCall this function to actually subscribe to the configured channel. This is usually invoked by the There are two possible events emitted by the Channel as a result:
|
this.subscribe = function() {
api.call('object/subscribe',
options,
function (err, res) {
if (err) {
event.emit('error', error('Subscribe failed with error: ' + err));
} else {
var i;
for (i=0; i<res.body.content.length; i++) {
self.objects[res.body.content[i].id] = res.body.content[i];
}
var objectKeys = Object.keys(self.objects);
for (i=0; i<objectKeys.length; i++) {
self.objects[objectKeys[i]].$$event = new EventObject(log);
}
monitor.add(options, self.objects, event, self.add, self.remove, self.update);
event.emit('subscribe');
}
});
};
|
¶ Channel.unsubscribeCall this function to unsubscribe from the configured channel. There are two possible events emitted by the Channel as a result:
|
this.unsubscribe = function() {
api.call('object/unsubscribe',
options,
function (err) {
if (err) {
event.emit('error', error('Unsubscribe failed with error: ' + err));
} else {
self.objects = {};
event.emit('unsubscribe');
event.emit('_unsubscribe');
}
});
};
|
¶ Channel.addAdd a new Telepat object to the current channel. The channel might emit an Params
object
Object
The new object to add
|
this.add = function(object) {
api.call('object/create',
{
model: options.channel.model,
context: options.channel.context,
content: object
},
function (err) {
if (err) {
event.emit('error', error('Adding object failed with error: ' + err));
} else {
}
});
};
|
¶ Channel.removeRemove a Telepat object from the current channel. The channel might emit an Instead of using this function, you can also delete the object from Params
id
integer
The id of the object to delete
|
this.remove = function(id) {
api.del('object/delete',
{
model: options.channel.model,
context: options.channel.context,
id: id
},
function (err) {
if (err) {
event.emit('error', error('Removing object failed with error: ' + err));
} else {
}
});
};
|
¶ Channel.updateUpdates a Telepat object from the current channel. The channel might emit an To call this function, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:
Instead of using this function, you can also update the object directly from Params
id
integer
The id of the object to update
patch
Array
The array of patches representing the modifications that need to be persisted.
|
this.update = function(id, patch) {
api.call('object/update',
{
model: options.channel.model,
context: options.channel.context,
id: id,
patches: patch
},
function (err) {
if (err) {
event.emit('error', error('Updating object failed with error: ' + err));
} else {
}
});
};
|
¶ Channel.onCall this function to add callbacks to be invoked on event triggers. Params
name
string
The name of the event to associate the callback with
callback
function
The callback to be executed
|
this.on = function(name, callback) {
return event.on(name, callback);
};
};
module.exports = Channel;
|