-
Notifications
You must be signed in to change notification settings - Fork 68
/
connect.js
94 lines (82 loc) · 2.17 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
/** @license MIT License (c) copyright 2011-2013 original author or authors */
/**
* 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
*
* @author Brian Cavalier
* @author John Hann
*/
(function(define) { 'use strict';
define(function(require) {
var all, connection;
all = require('when').all;
connection = require('./lib/connection');
return function connectPlugin(/* options */) {
var connections = [];
function makeConnection(sourceProxy, methodName, handler) {
connections.push(sourceProxy.advise(methodName, { on: handler }));
}
function connectFacet(wire, facet) {
var promises, connects;
connects = facet.options;
promises = Object.keys(connects).map(function(key) {
return connection.parse(
facet, key, connects[key], wire, makeConnection);
});
return all(promises);
}
return {
context: {
destroy: function(resolver) {
connection.removeAll(connections);
resolver.resolve();
}
},
facets: {
// A facet named "connect" that runs during the connect
// lifecycle phase
connect: {
connect: function(resolver, facet, wire) {
resolver.resolve(connectFacet(wire, facet));
}
}
}
};
};
});
}(typeof define == 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));