new Mediator(subjects)
A class which propagates the occurred events to the corresponding observer chain.
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
subjects |
Array.<caph.module.event.Subject> |
<optional> |
An array of the subject which represents the event type. |
- Since:
-
- 2.0.0
- See:
Throws:
Will throw an error if the subjects parameter is not an array of caph.module.event.Subject.
Example
var eventMediator = new caph.module.event.Mediator([ new caph.module.event.Subject('click'), new caph.module.event.Subject('keydown'), new caph.module.event.Subject('keyup') ]);
Methods
-
addObserver(type, observer) → {caph.module.event.Mediator}
-
Adds an observer to the subject which represents the given event type. If there is no subject, it will be created automatically.
Parameters:
Name Type Description type
String An event type.
observer
caph.module.mixins.event.Observer An observer to handle the event type.
- Since:
-
- 2.0.0
Returns:
Example
var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); var observer = new Observer(); var eventMediator = new caph.module.event.Mediator([ new caph.module.event.Subject('click') ]); eventMediator.addObserver('click', observer);
-
addSubject(subject) → {caph.module.event.Mediator}
-
Adds a subject which represents the event type. If already exist, do nothing.
Parameters:
Name Type Description subject
caph.module.event.Subject A subject to represent the event type.
- Since:
-
- 2.0.0
Throws:
Will throw an error if the subject parameter is not an instance of caph.module.event.Subject.
Returns:
Example
var eventMediator = new caph.module.event.Mediator([ new caph.module.event.Subject('click') ]); eventMediator.addSubject(new caph.module.event.Subject('mouseover')); eventMediator.addSubject(new caph.module.event.Subject('mouseout'));
-
removeObserver(type, observer) → {caph.module.event.Mediator}
-
Removes an observer from the subject which represents the given event type if exist.
Parameters:
Name Type Description type
String An event type.
observer
caph.module.mixins.event.Observer An observer to be removed from the event type.
- Since:
-
- 2.0.0
Returns:
Example
var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); var observer = new Observer(); var eventMediator = new caph.module.event.Mediator([ new caph.module.event.Subject('click') ]); eventMediator.addObserver('click', observer); eventMediator.removeObserver('click', observer);
-
send(message) → {caph.module.event.Mediator}
-
Sends a message to all observers in the corresponding subject.
Parameters:
Name Type Description message
caph.event.Messsage A message to be sent.
- Since:
-
- 2.0.0
Returns:
Example
var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); var observer = new Observer(); var eventMediator = new caph.module.event.Mediator([ new caph.module.event.Subject('click') ]); eventMediator.addObserver('click', observer); eventMediator.send(new caph.event.Message({ type: 'click', detail: { offsetX: 10, offsetY: 10 } });
-
sendTo(observer, message) → {caph.module.event.Mediator}
-
Sends a message to the designated observer in the corresponding subject. If the message propagation is stopped, will not propagate any more.
Parameters:
Name Type Description observer
caph.module.mixins.event.Observer An observer to receive a message.
message
caph.event.Messsage A message to be sent.
- Since:
-
- 2.0.0
Returns:
Example
var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); var observer = new Observer(); var eventMediator = new caph.module.event.Mediator([ new caph.module.event.Subject('click') ]); eventMediator.addObserver('click', observer); eventMediator.sendTo(observer, new caph.event.Message({ type: 'click', detail: { offsetX: 10, offsetY: 10 } });