forked from cujojs/wire
-
Notifications
You must be signed in to change notification settings - Fork 3
/
connect.js
113 lines (98 loc) · 3.09 KB
/
connect.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* wire/connect plugin
* wire plugin that can connect synthetic events (method calls) on one
* component to methods of another object. For example, connecting a
* view's onClick event (method) to a controller's _handleViewClick method:
*
* view: {
* create: 'myView',
* ...
* },
* controller: {
* create: 'myController',
* connect: {
* 'view.onClick': '_handleViewClick'
* }
* }
*
* It also supports arbitrary transforms on the data that flows over the
* connection.
*
* transformer: {
* module: 'myTransformFunction'
* },
* view: {
* create: 'myView',
* ...
* },
* controller: {
* create: 'myController',
* connect: {
* 'view.onClick': 'transformer | _handleViewClick'
* }
* }
*
* wire is part of the cujo.js family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*/
(function(define) {
define(['when', 'meld', './lib/functional', './lib/connection'],
function(when, meld, functional, connection) {
return {
wire$plugin: function eventsPlugin(ready, destroyed /*, options */) {
var connectHandles = [];
/**
* Create a single connection from source[event] to target[method] so that
* when source[event] is invoked, target[method] will be invoked afterward
* with the same params.
*
* @param source source object
* @param event source method
* @param handler {Function} function to invoke
*/
function doConnectOne(source, event, handler) {
return meld.on(source, event, handler);
}
function handleConnection(source, eventName, handler) {
connectHandles.push(doConnectOne(source, eventName, handler));
}
function doConnect(proxy, connect, options, wire) {
return connection.parse(proxy, connect, options, wire, handleConnection);
}
function connectFacet(wire, facet) {
var connect, promises, connects;
connects = facet.options;
promises = [];
for(connect in connects) {
promises.push(doConnect(facet, connect, connects[connect], wire));
}
return when.all(promises);
}
destroyed.then(function onContextDestroy() {
for (var i = connectHandles.length - 1; i >= 0; i--){
connectHandles[i].remove();
}
});
return {
facets: {
connect: {
connect: function(resolver, facet, wire) {
when.chain(connectFacet(wire, facet), resolver);
}
}
}
};
}
};
});
})(typeof define == 'function'
? define
: function(deps, factory) {
module.exports = factory.apply(this, deps.map(function(x) {
return require(x);
}));
}
);