Class baja.Subscriber
Extends
BaseBajaObj.
Component Subscriber used to subscribe to multiple Components for events.
Defined in: comp.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
| Method Attributes | Method Name and Description |
|---|---|
| <private> |
$ordSubscribe(obj, ords, space)
An internal private method for subscribing Components via their ORDs.
|
|
attach(event, func)
Attach an Event Handler to this Subscriber.
|
|
|
detach(hName, func)
Detach an Event Handler from the Subscriber.
|
|
|
Return an array of the Components currently being
subscribed to by this Subscriber.
|
|
|
getHandlers(hName)
Return an array of event handlers.
|
|
|
hasHandlers(hName)
Return true if there any handlers registered for the given handler name.
|
|
|
isSubscribed(comp)
Return true if the Component is subscribed in this Subscriber.
|
|
|
subscribe(obj)
Subscribe a Component or a number of Components.
|
|
|
unsubscribe(obj)
Unsubscribe a Component or a number of Components.
|
|
|
unsubscribeAll(obj)
Unsubscribe all Components from a Subscriber.
|
- Methods borrowed from class BaseBajaObj:
- equals, valueOf
- Parameters:
- {Object} obj Optional
- the Object Literal used for the method's arguments.
- {Array} ords
- an Array of String ORDs that should resolve to Components for subscription.
- {baja.ComponentSpace} space
- the Component Space used for ORD resolution.
- {Function} obj.ok Optional
- the ok callback. Called once the Components have been subscribed.
- {Function} obj.fail Optional
- the fail callback. Called if the Components fail to subscribe. Any errors will be passed to this function.
- {baja.comm.Batch} obj.batch Optional
- if defined, any network calls will be batched into this object.
A Subscriber can be used to subscribe to multiple Components in the Station and can be used to listen for Component Events. For instance, a common one would be a Property changed event...
// sub is a Subscriber and is subscribed to a Component
sub.attach("changed", function (prop, cx) {
if (prop.getName() === "out") {
baja.outln("The output of the point is: " + this.getOutDisplay());
}
});
An event handler consists of a name and a function. When the
function is called, 'this' will map to the target Component.
Here are some examples of the different event handlers that can be attached to a Component...
// Property Changed
sub.attach("changed", function (prop, cx) {
// prop: the Property that has changed
// cx: the Context (used internally)
});
// Property Added
sub.attach("added", function (prop, cx) {
// prop: the Property that has been added
// cx: the Context (used internally)
});
// Property Removed
sub.attach("removed", function (prop, val, cx) {
// prop: the Property that has been removed
// val: the old value of the Property
// cx: the Context (used internally)
});
// Property Renamed
sub.attach("renamed", function (prop, oldName, cx) {
// prop: the Property that has been renamed
// oldName: the old slot name
// cx: the Context (used internally)
});
// Dynamic Slots Reordered
sub.attach("reordered", function (cx) {
// cx: the Context (used internally)
});
// Topic Fired
sub.attach("topicFired", function (topic, event, cx) {
// topic: the Topic that has been fired
// event: the Topic event data (can be null)
// cx: the Context (used internally)
});
// Slot Flags Changed
sub.attach("flagsChanged", function (slot, cx) {
// slot: the slot whose flags have changed
// cx: the Context (used internally)
});
// Slot Facets Changed
sub.attach("facetsChanged", function (slot, cx) {
// slot: the slot whose facets have changed
// cx: the Context (used internally)
});
// Component subscribed
sub.attach("subscribed", function (cx) {
// cx: the Context (used internally)
});
// Component unsubscribed
sub.attach("unsubscribed", function (cx) {
// cx: the Context (used internally)
});
// Component unmounted (called just before Component is removed from parent)
sub.attach("unmount", function (cx) {
// cx: the Context (used internally)
});
// Component renamed in parent
sub.attach("componentRenamed", function (oldName, cx) {
// cx: the Context (used internally)
});
// Component's flags changed in parent
sub.attach("componentFlagsChanged", function (cx) {
// cx: the Context (used internally)
});
// Component's facets changed in parent
sub.attach("componentFacetsChanged", function (cx) {
// cx: the Context (used internally)
});
// Component reordered in parent
sub.attach("componentReordered", function (cx) {
// cx: the Context (used internally)
});
An Object Literal can be used to specify multiple handlers. For example...
var sub = new baja.Subscriber();
sub.attach({
changed: function (prop, cx) {
},
subscribed: function (cx) {
}
});
Spaces can be used in a name to specify a function for multiple events...
var sub = new baja.Subscriber();
sub.attach("subscribed changed", function () {
updateGui(this);
});
- See:
- baja.Component#attach
- baja.Subscriber#detach
- baja.Subscriber#getHandlers
- baja.Subscriber#hasHandlers
- baja.Slot
If no arguments are used with this method then all events are removed.
For a list of all the event handlers, please see baja.Subscriber#attach.
A String with spaces can be supplied to remove multiple event handlers. For example...
sub.detach("subscribed changed"); // Remove all subscribed and changed event handlers
- Parameters:
- {String} hName Optional
- the name of the handler to detach from the Subscriber.
- {Function} func Optional
- the function to remove from the Subscriber. It's recommended to supply this just in case other scripts have added event handlers.
- See:
- baja.Component#attach
- baja.Subscriber#attach
- baja.Subscriber#getHandlers
- baja.Subscriber#hasHandlers
- Returns:
- {Array} a copy of the array used to subscribe Components (baja.Component).
For a list of all the event handlers, please see baja.Subscriber#attach.
To access multiple handlers, insert a space between the handler names.
- Parameters:
- {String} hName
- the name of the handler
- Returns:
- {Array}
- See:
- baja.Component#attach
- baja.Subscriber#detach
- baja.Subscriber#attach
- baja.Subscriber#hasHandlers
If no handler name is specified then test to see if there are any handlers registered at all.
For a list of all the event handlers, please see baja.Subscriber#attach.
Multiple handlers can be tested for by using a space character between the names.
- Parameters:
- {String} hName Optional
- the name of the handler. If undefined, then see if there are any handlers registered at all.
- Returns:
- {Boolean}
- See:
- baja.Component#attach
- baja.Subscriber#detach
- baja.Subscriber#attach
- baja.Subscriber#getHandlers
- Parameters:
- {Object} comp
- the Component to be tested for Subscription.
- Returns:
- {Boolean}
This will put the Components into subscription if they are not already subscribed and are mounted. The Subscription will last until the page is refreshed or unsubscribe is called.
If the Components are mounted and able to be subscribed, this will result in an asynchronous network call.
A Component instance, array of Components or an optional Object Literal can be used to specify the method's arguments...
// Subscribe a single Component
sub.subscribe(aComp);
// ...or subscribe an array of Components...
sub.subscribe([aComp1, aComp2]);
// ...or use an Object Literal for more arguments...
sub.subscribe({
comps: [aComp1, aComp2], // Can also just be an singular Component instance
ok: function () {
// Called once the Components are subscribed (optional)
},
fail: function (err) {
// Called if the Components fail to subscribe (optional)
},
batch // if defined, any network calls will be batched into this object (optional)
});
For callbacks, the 'this' keyword is set to the comps property.
- Parameters:
- {Object} obj Optional
- the Object Literal used for the method's arguments.
- {Function} obj.ok Optional
- the ok callback. Called once the Components have been subscribed.
- {Function} obj.fail Optional
- the fail callback. Called if the Components fail to subscribe. Any errors will be passed to this function.
- {baja.comm.Batch} obj.batch Optional
- if defined, any network calls will be batched into this object.
This will unsubscribe the mounted Components if they are not already unsubscribed.
If the Components are able to be unsubscribed, this will result in an asynchronous network call.
A Component instance, array of Components or an optional Object Literal can be used to specify the method's arguments...
// Unsubscribe a single Component
sub.unsubscribe(aComp);
// ...or unsubscribe an array of Components...
sub.unsubscribe([aComp1, aComp2]);
// ...or use an Object Literal for more arguments...
sub.unsubscribe({
comps: [aComp1, aComp2], // Can also just be an singular Component instance
ok: function () {
// Called once the Components are unsubscribed (optional)
},
fail: function (err) {
// Called if the Components fail to unsubscribe (optional)
},
batch // if defined, any network calls will be batched into this object (optional)
});
For callbacks, the 'this' keyword is set to the comps property.
- Parameters:
- {Object} obj Optional
- the Object Literal used for the method's arguments.
- {Function} obj.ok Optional
- the ok callback. Called once the Components have been unsubscribed.
- {Function} obj.fail Optional
- the fail callback. Called if the Components fail to subscribe. Any errors will be passed to this function.
- {baja.comm.Batch} obj.batch Optional
- if defined, any network calls will be batched into this object.
This will unregister all Components from this Subscriber.
If the Components are able to be unsubscribed, this will result in an asynchronous network call.
An Object Literal is used to specify the method's arguments...
// ...or use an Object Literal for more arguments...
sub.unsubscribeAll({
ok: function () {
// Called once the Components are unsubscribed (optional)
},
fail: function (err) {
// Called if the Components fail to unsubscribe (optional)
},
batch // if defined, any network calls will be batched into this object (optional)
});
For callbacks, the 'this' keyword is set to the internal component array.
- Parameters:
- {Object} obj Optional
- the Object Literal used for the method's arguments.
- {Function} obj.ok Optional
- the ok callback. Called once the Components have been unsubscribed.
- {Function} obj.fail Optional
- the fail callback. Called if the Components fail to subscribe. Any errors will be passed to this function.
- {baja.comm.Batch} obj.batch Optional
- if defined, any network calls will be batched into this object.