-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEventMapper.js
55 lines (49 loc) · 1.7 KB
/
EventMapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var EventHandler = require('famous/core/EventHandler');
/**
* EventMapper routes events to various event destinations
* based on custom logic. The function signature is arbitrary.
*
* @class EventMapper
* @constructor
*
* @param {function} mappingFunction function to determine where
* events are routed to.
*/
function EventMapper(mappingFunction) {
EventHandler.call(this);
this._mappingFunction = mappingFunction;
}
EventMapper.prototype = Object.create(EventHandler.prototype);
EventMapper.prototype.constructor = EventMapper;
EventMapper.prototype.subscribe = null;
EventMapper.prototype.unsubscribe = null;
/**
* Trigger an event, sending to all mapped downstream handlers
* listening for provided 'type' key.
*
* @method emit
*
* @param {string} type event type key (for example, 'click')
* @param {Object} data event data
* @return {EventHandler} this
*/
EventMapper.prototype.emit = function emit(type, data) {
var target = this._mappingFunction.apply(this, arguments);
if (target && (target.emit instanceof Function)) target.emit(type, data);
};
/**
* Alias of emit.
* @method trigger
*/
EventMapper.prototype.trigger = EventMapper.prototype.emit;
module.exports = EventMapper;
});