1 | module.exports = create;
|
2 |
|
3 | function create (brick, selector, source, binder) {
|
4 | if (typeof selector == 'object') {
|
5 | return each(brick, selector);
|
6 | }
|
7 |
|
8 | if (!brick.bindings) brick.bindings = {};
|
9 |
|
10 | var options = source;
|
11 | var selectorParts;
|
12 | var opt;
|
13 |
|
14 | if (options.isBrickTemplate) {
|
15 | options = {
|
16 | _html: options[0]
|
17 | };
|
18 | }
|
19 |
|
20 | if (Array.isArray(options)) {
|
21 | options = attachAll(brick, options);
|
22 | }
|
23 |
|
24 | if (options.embedsBrick) {
|
25 | options = {
|
26 | _html: brick.attach(options.brick).element[0]
|
27 | };
|
28 | }
|
29 |
|
30 | if (options._html && options._html.embedsBrick) {
|
31 | options._html = brick.attach(options._html).element[0];
|
32 | }
|
33 |
|
34 | if (options.returnsTemplate) {
|
35 | options = {
|
36 | _html: options()
|
37 | };
|
38 | }
|
39 |
|
40 | if (typeof options == 'function') {
|
41 | options = options();
|
42 | }
|
43 |
|
44 | if (binder && brick.element[0]) {
|
45 | binder(brick.element[0][0], selector, source);
|
46 | }
|
47 |
|
48 | brick.bindings[selector] = {
|
49 | selector: selector,
|
50 | source: source,
|
51 | binder: binder,
|
52 | options: options
|
53 | };
|
54 | }
|
55 |
|
56 | function attachAll (brick, list) {
|
57 | var i = -1;
|
58 | var result = list.slice();
|
59 | var len = result.length;
|
60 |
|
61 | while (++i < len) {
|
62 | if (!result[i].embedsBrick) {
|
63 | continue;
|
64 | }
|
65 |
|
66 | brick.attach(result[i]);
|
67 |
|
68 | result[i] = {
|
69 | ':first': {
|
70 | 'data-id': result[i].brick.id,
|
71 | '_html': result[i].brick.element[0]
|
72 | }
|
73 | };
|
74 | }
|
75 |
|
76 | return result;
|
77 | }
|
78 |
|
79 | function each (brick, bindings) {
|
80 | var selector;
|
81 | for (selector in bindings) {
|
82 | create(brick, selector, bindings[selector]);
|
83 | }
|
84 | }
|