breeze-client / BreezeEvent
Class: BreezeEvent<T>
Defined in: src/core/event.ts:50
Class to support basic event publication and subscription semantics.
Type Parameters
T
T
Constructors
Constructor
new BreezeEvent<
T>(name,publisher,defaultErrorCallback?):BreezeEvent<T>
Defined in: src/core/event.ts:112
Constructor for an Event
salaryEvent = new BreezeEvent("salaryEvent", person);Parameters
name
string
The name of the event.
publisher
Object
The object that will be doing the publication. i.e. the object to which this event is attached.
defaultErrorCallback?
(e) => any
Function to call when an error occurs during subscription execution. If omitted then subscriber notification failures will be ignored.
Returns
BreezeEvent<T>
Properties
name
name:
string
Defined in: src/core/event.ts:78
The name of this Event
publisher
publisher:
Object
Defined in: src/core/event.ts:80
The object doing the publication. i.e. the object to which this event is attached.
unhandledErrorCallback
staticunhandledErrorCallback: ((e) =>void) |null
Defined in: src/core/event.ts:72
Called with any exception a subscriber throws that nothing else has handled - that is, when neither publish nor the event was given an error callback.
It logs to console.error by default. Set your own to route it into a logger, or null to go back to discarding these silently (which is what Breeze did before 3.0, and why handler bugs were so hard to find).
BreezeEvent.unhandledErrorCallback = e => myLogger.error(e);
BreezeEvent.unhandledErrorCallback = null; // say nothingMethods
clear()
clear():
void
Defined in: src/core/event.ts:256
remove all subscribers
Returns
void
publish()
publish(
data,publishAsync?,errorCallback?):boolean
Defined in: src/core/event.ts:155
Publish data for this event.
// Assume 'salaryEvent' is previously constructed Event
salaryEvent.publish( { eventType: "payRaise", amount: 100 });This event can also be published asychronously
salaryEvent.publish( { eventType: "payRaise", amount: 100 }, true);And we can add a handler in case the subscriber 'mishandles' the event.
salaryEvent.publish( { eventType: "payRaise", amount: 100 }, true, function(error) {
// do something with the 'error' object
});Parameters
data
T
Data to publish
publishAsync?
boolean = false
(default=false) Whether to publish asynchonously or not.
errorCallback?
(e) => any
Function to be called for any errors that occur during publication. If omitted, errors will be eaten.
Returns
boolean
false if event is disabled; true otherwise.
publishAsync()
publishAsync(
data,errorCallback):void
Defined in: src/core/event.ts:184
Publish data for this event asynchronously.
// Assume 'salaryEvent' is previously constructed Event
salaryEvent.publishAsync( { eventType: "payRaise", amount: 100 });And we can add a handler in case the subscriber 'mishandles' the event.
salaryEvent.publishAsync( { eventType: "payRaise", amount: 100 }, function(error) {
// do something with the 'error' object
});Parameters
data
T
Data to publish
errorCallback
(e) => any
Function to be called for any errors that occur during publication. If omitted, errors will be eaten.
Returns
void
subscribe()
subscribe(
callback):number
Defined in: src/core/event.ts:211
Subscribe to this event.
// Assume 'salaryEvent' is previously constructed Event
salaryEvent.subscribe(function (eventArgs) {
if (eventArgs.eventType === "payRaise") {
// do something
}
});There are several built in Breeze events, such as EntityAspect.propertyChanged, EntityAspect.validationErrorsChanged as well.
// Assume order is a preexisting 'order' entity
order.entityAspect.propertyChanged.subscribe(function (pcEvent) {
if ( pcEvent.propertyName === "OrderDate") {
// do something
}
});Parameters
callback
(data) => any
Called whenever data is published for this event, with that data as its argument. What is published is documented on each specific event.
Returns
number
This is a key for 'unsubscription'. It can be passed to the 'unsubscribe' method.
unsubscribe()
unsubscribe(
unsubKey):boolean
Defined in: src/core/event.ts:238
Unsubscribe from this event.
// Assume order is a preexisting 'order' entity
let token = order.entityAspect.propertyChanged.subscribe(function (pcEvent) {
// do something
});
// sometime later
order.entityAspect.propertyChanged.unsubscribe(token);Parameters
unsubKey
number
The value returned from the 'subscribe' method may be used to unsubscribe here.
Returns
boolean
Whether unsubscription occured. This will return false if already unsubscribed or if the key simply cannot be found.
bubbleEvent()
staticbubbleEvent(target,getParentFn?):void
Defined in: src/core/event.ts:263
event bubbling - document later.
Parameters
target
any
getParentFn?
() => any
Returns
void
enable()
staticenable(eventName,obj,isEnabled):void
Defined in: src/core/event.ts:304
Enables or disables the named event for an object and all of its children.
BreezeEvent.enable(“propertyChanged”, myEntityManager, false)will disable all EntityAspect.propertyChanged events within a EntityManager.
BreezeEvent.enable(“propertyChanged”, myEntityManager, true)will enable all EntityAspect.propertyChanged events within a EntityManager.
BreezeEvent.enable(“propertyChanged”, myEntity.entityAspect, false)will disable EntityAspect.propertyChanged events for a specific entity.
BreezeEvent.enable(“propertyChanged”, myEntity.entityAspect, null)will removes any enabling / disabling at the entity aspect level so now any 'Event.enable' calls at the EntityManager level, made either previously or in the future, will control notification.
BreezeEvent.enable(“validationErrorsChanged”, myEntityManager, function(em) {
return em.customTag === “blue”;
})will either enable or disable myEntityManager based on the current value of a ‘customTag’ property on myEntityManager. Note that this is dynamic, changing the customTag value will cause events to be enabled or disabled immediately.
Parameters
eventName
string
The name of the event.
obj
Object
The object at which enabling or disabling will occur. All event notifications that occur to this object or children of this object will be enabled or disabled.
isEnabled
boolean | ((x) => boolean)
A boolean, a null or a function that returns either a boolean or a null.
Returns
void
isEnabled()
staticisEnabled(eventName,obj):boolean
Defined in: src/core/event.ts:325
Returns whether for a specific event and a specific object and its children, notification is enabled or disabled or not set.
BreezeEvent.isEnabled(“propertyChanged”, myEntityManager)Parameters
eventName
string
The name of the event.
obj
Object
The object for which we want to know if notifications are enabled.
Returns
boolean
Whether notifications are enabled. If nothing is set on the object, the answer comes from its parent chain, and defaults to true.