Event Emitter
This is the heart of Jodit. All processes inside Jodit mostly flow through events.
const jodit = Jodit.make('#editor');
jodit.e.on('someEvent', param => alert(`Some event fired for ${param}!!!`));
jodit.async.setTimeout(() => {
jodit.e.fire('someEvent', 'you');
}, 1000);
In addition to custom events, the emitter can work with DOM elements and browser events:
jodit.e.on(document.body, 'click', () => alert(`Doc click!!!`));
If you don't want to listen to the event anymore, you can unsubscribe from it:
jodit.e
.one('someEvent', param => alert(`Some event fired for ${param}!!!`))
.one('someEvent', param => alert(`Some event fired for ${param}!!!`))
.one('someEvent', param => alert(`Some event fired for ${param}!!!`));
jodit.e.off('someEvent'); // will unsubscribe from all handlers
const handler1 = param => alert(`Some event fired for ${param}!!!`);
const handler2 = param => alert(`Some event fired for ${param}!!!`);
const handler3 = param => alert(`Some event fired for ${param}!!!`);
jodit.e
.one('someEvent', handler1)
.one('someEvent', handler2)
.one('someEvent', handler3);
jodit.e.off('someEvent', handler1); // only handler1 will unsubscribe
Classes
defaultNameSpace
Const
defaultNameSpace: "JoditEventDefaultNamespace"
Defined in
jodit/src/core/event-emitter/store.ts:19
observable
observable<T
, O
>(obj
): O
Makes any object an observable object
Type parameters
Name | Type |
---|---|
T |
extends IDictionary |
O |
extends IDictionary & IObservable |
Parameters
Name | Type |
---|---|
obj |
T |
Returns
O
Example
const obj = {
a: 1,
b: {
c: 5
}
}
const obsObj = Jodit.modules.observable(obj);
console.log(obj === obsObj); // true
obsObj.on('change', () => {
console.log('Object changed');
});
obsObj.on('change.a', () => {
console.log('Key a changed');
});
obsObj.on('change.b.c', () => {
console.log('Key b.c changed');
});
obj.a = 6;
// Object changed
// Key a changed
obj.b = {c: 6}
// Object changed
obj.b.c = 8
// Object changed
// Key b.c changed
Defined in
jodit/src/core/event-emitter/observable.ts:65