Skip to content

Commit 97dc41c

Browse files
committed
Allow connecting to multiple methods using an array.
1 parent 469cf83 commit 97dc41c

File tree

3 files changed

+373
-14
lines changed

3 files changed

+373
-14
lines changed

docs/connections.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,34 @@ define({
267267
});
268268
```
269269

270+
Connections can also be made to more than one method. Use an array of Strings instead of a single String to invoke multiple methods:
271+
272+
```js
273+
define({
274+
$plugins: [
275+
{ module: 'wire/connect'},
276+
// other plugins ...
277+
],
278+
279+
component1: {
280+
create: // ...
281+
connect: {
282+
// Whenever component2.doSomething is called,
283+
// component1.doSomethingAlso1 and component1.doSomethingAlso2 will also
284+
// be invoked, with the same parameters.
285+
'component2.doSomething': [
286+
'doSomethingAlso1',
287+
'doSomethingAlso2'
288+
]
289+
}
290+
},
291+
292+
component2: {
293+
create: // ...
294+
}
295+
});
296+
```
297+
270298
# Aspect Oriented Programming (AOP)
271299

272300
**Plugin:** wire/aop
@@ -304,7 +332,16 @@ define({
304332
// component2.doSomething returns (but not if it throws, see
305333
// afterThrowing below). The return value of component2.doSomething
306334
// will be passed to component1.doSomethingAfterReturning
307-
doSomething: 'component1.doSomethingAfterReturning'
335+
doSomething: 'component1.doSomethingAfterReturning',
336+
337+
// component1.doSomething1 and component1.doSomething2 will be
338+
// invoked after component2.doSomethingElse returns. The return
339+
// value of component2.doSomething will be passed to both
340+
// component1.doSomething1 and component1.doSomething2.
341+
doSomethingElse: [
342+
'component1.doSomething1',
343+
'component1.doSomething2'
344+
]
308345
},
309346

310347
afterThrowing: {

lib/connection.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,24 @@ define(function(require) {
104104
* rejects if an error occurs.
105105
*/
106106
function parseIncoming(source, eventName, targetProxy, connect, options, wire, createConnection) {
107-
var promise, methodName;
107+
var promise, methodName, methodNames;
108108

109109
if(eventName) {
110110
// 'component.eventName': 'methodName'
111111
// 'component.eventName': 'transform | methodName'
112112

113113
methodName = options;
114+
methodNames = Array.isArray(options) ? options : [ options ];
114115

115-
promise = pipeline(targetProxy, methodName, wire).then(
116-
function(func) {
117-
var invoker = proxyInvoker(targetProxy, func);
116+
promise = when.map(methodNames, function(methodName) {
117+
return pipeline(targetProxy, methodName, wire).then(
118+
function(func) {
119+
var invoker = proxyInvoker(targetProxy, func);
118120

119-
return createConnection(source, eventName, invoker);
120-
}
121-
);
121+
return createConnection(source, eventName, invoker);
122+
}
123+
);
124+
});
122125

123126
} else {
124127
// componentName: {
@@ -190,10 +193,6 @@ define(function(require) {
190193
promise = connectOneOutgoing(targetProxy, options);
191194

192195
} else {
193-
// eventName: {
194-
// componentName: 'methodName'
195-
// componentName: 'transform | methodName'
196-
// }
197196
promises = [];
198197

199198
resolveAndConnectOneOutgoing = function(targetRef, targetMethodSpec) {
@@ -202,8 +201,20 @@ define(function(require) {
202201
});
203202
};
204203

205-
for(name in options) {
206-
promises.push(resolveAndConnectOneOutgoing(name, options[name]));
204+
if (Array.isArray(options)) {
205+
// eventName: [ 'methodName1', 'methodName2' ]
206+
for (var i = 0, t = options.length; i < t; i++) {
207+
promises.push(connectOneOutgoing(targetProxy, options[i]));
208+
}
209+
210+
} else {
211+
// eventName: {
212+
// componentName: 'methodName'
213+
// componentName: 'transform | methodName'
214+
// }
215+
for(name in options) {
216+
promises.push(resolveAndConnectOneOutgoing(name, options[name]));
217+
}
207218
}
208219

209220
promise = when.all(promises);

0 commit comments

Comments
 (0)