Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow connecting to multiple methods using an array. #172

Merged
merged 2 commits into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,34 @@ define({
});
```

Connections can also be made to more than one method. Use an array of Strings instead of a single String to invoke multiple methods:

```js
define({
$plugins: [
{ module: 'wire/connect'},
// other plugins ...
],

component1: {
create: // ...
connect: {
// Whenever component2.doSomething is called,
// component1.doSomethingAlso1 and component1.doSomethingAlso2 will also
// be invoked, with the same parameters.
'component2.doSomething': [
'doSomethingAlso1',
'doSomethingAlso2'
]
}
},

component2: {
create: // ...
}
});
```

# Aspect Oriented Programming (AOP)

**Plugin:** wire/aop
Expand Down Expand Up @@ -304,7 +332,16 @@ define({
// component2.doSomething returns (but not if it throws, see
// afterThrowing below). The return value of component2.doSomething
// will be passed to component1.doSomethingAfterReturning
doSomething: 'component1.doSomethingAfterReturning'
doSomething: 'component1.doSomethingAfterReturning',

// component1.doSomething1 and component1.doSomething2 will be
// invoked after component2.doSomethingElse returns. The return
// value of component2.doSomething will be passed to both
// component1.doSomething1 and component1.doSomething2.
doSomethingElse: [
'component1.doSomething1',
'component1.doSomething2'
]
},

afterThrowing: {
Expand Down
39 changes: 24 additions & 15 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,30 @@ define(function(require) {
* rejects if an error occurs.
*/
function parseIncoming(source, eventName, targetProxy, connect, options, wire, createConnection) {
var promise, methodName;
var promise, methodNames;

if(eventName) {
// 'component.eventName': 'methodName'
// 'component.eventName': 'transform | methodName'

methodName = options;
methodNames = Array.isArray(options) ? options : [ options ];

promise = pipeline(targetProxy, methodName, wire).then(
function(func) {
var invoker = proxyInvoker(targetProxy, func);
promise = when.map(methodNames, function(methodName) {
return pipeline(targetProxy, methodName, wire).then(
function(func) {
var invoker = proxyInvoker(targetProxy, func);

return createConnection(source, eventName, invoker);
}
);
return createConnection(source, eventName, invoker);
}
);
});

} else {
// componentName: {
// eventName: 'methodName'
// eventName: 'transform | methodName'
// }

source = methodName;
promise = wire.getProxy(connect).then(function(srcProxy) {
var name, promises;

Expand Down Expand Up @@ -190,10 +191,6 @@ define(function(require) {
promise = connectOneOutgoing(targetProxy, options);

} else {
// eventName: {
// componentName: 'methodName'
// componentName: 'transform | methodName'
// }
promises = [];

resolveAndConnectOneOutgoing = function(targetRef, targetMethodSpec) {
Expand All @@ -202,8 +199,20 @@ define(function(require) {
});
};

for(name in options) {
promises.push(resolveAndConnectOneOutgoing(name, options[name]));
if (Array.isArray(options)) {
// eventName: [ 'methodName1', 'methodName2' ]
for (var i = 0, t = options.length; i < t; i++) {
promises.push(connectOneOutgoing(targetProxy, options[i]));
}

} else {
// eventName: {
// componentName: 'methodName'
// componentName: 'transform | methodName'
// }
for(name in options) {
promises.push(resolveAndConnectOneOutgoing(name, options[name]));
}
}

promise = when.all(promises);
Expand Down
Loading