This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdispatcher_auto.js
64 lines (54 loc) · 1.8 KB
/
dispatcher_auto.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
56
57
58
59
60
61
62
63
64
/**
* @fileoverview Instantiates a jsaction.Dispatcher and connects it with an
* instance of jsaction.EventContract that it receives from the main HTML page.
*/
goog.provide('jsaction.dispatcherAuto');
goog.require('jsaction.Dispatcher');
/**
* Registers a jsaction handler.
* @param {string} action
* @param {function(this:Element,Event)} handler
* @param {Object=} opt_instance
* @this jsaction.Dispatcher
*/
function register(action, handler, opt_instance) {
var separatorIndex = action.indexOf('.');
var namespace = action.substr(0, separatorIndex);
var actionName = action.substr(separatorIndex + 1);
var handlerMap = {};
handlerMap[actionName] = function(actionFlow) {
handler.call(actionFlow.node(), actionFlow.event());
};
this.registerHandlers(namespace, opt_instance || null, handlerMap);
}
/**
* Unregisters a jsaction handler.
* @param {string} action
* @this jsaction.Dispatcher
*/
function unregister(action) {
var separatorIndex = action.indexOf('.');
var namespace = action.substr(0, separatorIndex);
var actionName = action.substr(separatorIndex + 1);
this.unregisterHandler(namespace, actionName);
}
/**
* Creates a dispatcher and exposes a public API.
* @param {!Object} global
*/
function main(global) {
// If we can't find the exported jsaction namespace, it means we don't have an
// available contract.
if (!global['jsaction']) {
return;
}
// Binds a dispatcher to the contract.
var dispatcher = new jsaction.Dispatcher();
global['jsaction']['__dispatchTo'](
goog.bind(dispatcher.dispatch, dispatcher));
// Exposes JsAction's public API.
goog.exportSymbol('jsaction.register', goog.bind(register, dispatcher));
goog.exportSymbol('jsaction.unregister', goog.bind(unregister, dispatcher));
}
// Bootstraps the dispatcher.
main(goog.global);