d0b185b9 by Michael Richards

Merge pull request #80 from mikeric/advanced-custom-binders-api

Advanced custom binders API
2 parents 4093da62 9b103172
...@@ -18,8 +18,8 @@ describe('Rivets.Binding', function() { ...@@ -18,8 +18,8 @@ describe('Rivets.Binding', function() {
18 model = binding.model; 18 model = binding.model;
19 }); 19 });
20 20
21 it('gets assigned the routine function matching the identifier', function() { 21 it('gets assigned the proper binder routine matching the identifier', function() {
22 expect(binding.routine).toBe(rivets.routines.text); 22 expect(binding.binder.routine).toBe(rivets.binders.text);
23 }); 23 });
24 24
25 describe('bind()', function() { 25 describe('bind()', function() {
...@@ -72,50 +72,24 @@ describe('Rivets.Binding', function() { ...@@ -72,50 +72,24 @@ describe('Rivets.Binding', function() {
72 72
73 describe('set()', function() { 73 describe('set()', function() {
74 it('performs the binding routine with the supplied value', function() { 74 it('performs the binding routine with the supplied value', function() {
75 spyOn(binding, 'routine'); 75 spyOn(binding.binder, 'routine');
76 binding.set('sweater'); 76 binding.set('sweater');
77 expect(binding.routine).toHaveBeenCalledWith(el, 'sweater'); 77 expect(binding.binder.routine).toHaveBeenCalledWith(el, 'sweater');
78 }); 78 });
79 79
80 it('applies any formatters to the value before performing the routine', function() { 80 it('applies any formatters to the value before performing the routine', function() {
81 rivets.formatters.awesome = function(value) { return 'awesome ' + value }; 81 rivets.formatters.awesome = function(value) { return 'awesome ' + value };
82 binding.formatters.push('awesome'); 82 binding.formatters.push('awesome');
83 spyOn(binding, 'routine'); 83 spyOn(binding.binder, 'routine');
84 binding.set('sweater'); 84 binding.set('sweater');
85 expect(binding.routine).toHaveBeenCalledWith(el, 'awesome sweater'); 85 expect(binding.binder.routine).toHaveBeenCalledWith(el, 'awesome sweater');
86 }); 86 });
87 87
88 it('calls methods with the object as context', function() { 88 it('calls methods with the object as context', function() {
89 binding.model = {foo: 'bar'}; 89 binding.model = {foo: 'bar'};
90 spyOn(binding, 'routine'); 90 spyOn(binding.binder, 'routine');
91 binding.set(function() { return this.foo; }); 91 binding.set(function() { return this.foo; });
92 expect(binding.routine).toHaveBeenCalledWith(el, binding.model.foo); 92 expect(binding.binder.routine).toHaveBeenCalledWith(el, binding.model.foo);
93 });
94
95 describe('on an event binding', function() {
96 beforeEach(function() {
97 binding.options.special = 'event';
98 });
99
100 it('performs the binding routine with the supplied function and current listener', function() {
101 spyOn(binding, 'routine');
102 func = function() { return 1 + 2; }
103 binding.set(func);
104 expect(binding.routine).toHaveBeenCalledWith(el, binding.model, func, undefined);
105 });
106 });
107
108 describe('on an iteration binding', function(){
109 beforeEach(function(){
110 binding.options.special = 'iteration';
111 });
112
113 it('performs the binding routine with the supplied collection and binding', function() {
114 spyOn(binding, 'routine');
115 array = [{name: 'a'}, {name: 'b'}];
116 binding.set(array);
117 expect(binding.routine).toHaveBeenCalledWith(el, array, binding);
118 });
119 }); 93 });
120 }); 94 });
121 95
......
...@@ -18,13 +18,13 @@ describe('Routines', function() { ...@@ -18,13 +18,13 @@ describe('Routines', function() {
18 18
19 describe('text', function() { 19 describe('text', function() {
20 it("sets the element's text content", function() { 20 it("sets the element's text content", function() {
21 rivets.routines.text(el, '<em>gluten-free</em>'); 21 rivets.binders.text(el, '<em>gluten-free</em>');
22 expect(el.textContent || el.innerText).toBe('<em>gluten-free</em>'); 22 expect(el.textContent || el.innerText).toBe('<em>gluten-free</em>');
23 expect(el.innerHTML).toBe('&lt;em&gt;gluten-free&lt;/em&gt;'); 23 expect(el.innerHTML).toBe('&lt;em&gt;gluten-free&lt;/em&gt;');
24 }); 24 });
25 25
26 it("sets the element's text content to zero when a numeric zero is passed", function() { 26 it("sets the element's text content to zero when a numeric zero is passed", function() {
27 rivets.routines.text(el, 0); 27 rivets.binders.text(el, 0);
28 expect(el.textContent || el.innerText).toBe('0'); 28 expect(el.textContent || el.innerText).toBe('0');
29 expect(el.innerHTML).toBe('0'); 29 expect(el.innerHTML).toBe('0');
30 }); 30 });
...@@ -32,13 +32,13 @@ describe('Routines', function() { ...@@ -32,13 +32,13 @@ describe('Routines', function() {
32 32
33 describe('html', function() { 33 describe('html', function() {
34 it("sets the element's HTML content", function() { 34 it("sets the element's HTML content", function() {
35 rivets.routines.html(el, '<strong>fixie</strong>'); 35 rivets.binders.html(el, '<strong>fixie</strong>');
36 expect(el.textContent || el.innerText).toBe('fixie'); 36 expect(el.textContent || el.innerText).toBe('fixie');
37 expect(el.innerHTML).toBe('<strong>fixie</strong>'); 37 expect(el.innerHTML).toBe('<strong>fixie</strong>');
38 }); 38 });
39 39
40 it("sets the element's HTML content to zero when a zero value is passed", function() { 40 it("sets the element's HTML content to zero when a zero value is passed", function() {
41 rivets.routines.html(el, 0); 41 rivets.binders.html(el, 0);
42 expect(el.textContent || el.innerText).toBe('0'); 42 expect(el.textContent || el.innerText).toBe('0');
43 expect(el.innerHTML).toBe('0'); 43 expect(el.innerHTML).toBe('0');
44 }); 44 });
...@@ -46,17 +46,17 @@ describe('Routines', function() { ...@@ -46,17 +46,17 @@ describe('Routines', function() {
46 46
47 describe('value', function() { 47 describe('value', function() {
48 it("sets the element's value", function() { 48 it("sets the element's value", function() {
49 rivets.routines.value(input, 'pitchfork'); 49 rivets.binders.value.routine(input, 'pitchfork');
50 expect(input.value).toBe('pitchfork'); 50 expect(input.value).toBe('pitchfork');
51 }); 51 });
52 52
53 it("applies a default value to the element when the model doesn't contain it", function() { 53 it("applies a default value to the element when the model doesn't contain it", function() {
54 rivets.routines.value(input, undefined); 54 rivets.binders.value.routine(input, undefined);
55 expect(input.value).toBe(''); 55 expect(input.value).toBe('');
56 }); 56 });
57 57
58 it("sets the element's value to zero when a zero value is passed", function() { 58 it("sets the element's value to zero when a zero value is passed", function() {
59 rivets.routines.value(input, 0); 59 rivets.binders.value.routine(input, 0);
60 expect(input.value).toBe('0'); 60 expect(input.value).toBe('0');
61 }); 61 });
62 }); 62 });
...@@ -64,14 +64,14 @@ describe('Routines', function() { ...@@ -64,14 +64,14 @@ describe('Routines', function() {
64 describe('show', function() { 64 describe('show', function() {
65 describe('with a truthy value', function() { 65 describe('with a truthy value', function() {
66 it('shows the element', function() { 66 it('shows the element', function() {
67 rivets.routines.show(el, true); 67 rivets.binders.show(el, true);
68 expect(el.style.display).toBe(''); 68 expect(el.style.display).toBe('');
69 }); 69 });
70 }); 70 });
71 71
72 describe('with a falsey value', function() { 72 describe('with a falsey value', function() {
73 it('hides the element', function() { 73 it('hides the element', function() {
74 rivets.routines.show(el, false); 74 rivets.binders.show(el, false);
75 expect(el.style.display).toBe('none'); 75 expect(el.style.display).toBe('none');
76 }); 76 });
77 }); 77 });
...@@ -80,14 +80,14 @@ describe('Routines', function() { ...@@ -80,14 +80,14 @@ describe('Routines', function() {
80 describe('hide', function() { 80 describe('hide', function() {
81 describe('with a truthy value', function() { 81 describe('with a truthy value', function() {
82 it('hides the element', function() { 82 it('hides the element', function() {
83 rivets.routines.hide(el, true); 83 rivets.binders.hide(el, true);
84 expect(el.style.display).toBe('none'); 84 expect(el.style.display).toBe('none');
85 }); 85 });
86 }); 86 });
87 87
88 describe('with a falsey value', function() { 88 describe('with a falsey value', function() {
89 it('shows the element', function() { 89 it('shows the element', function() {
90 rivets.routines.hide(el, false); 90 rivets.binders.hide(el, false);
91 expect(el.style.display).toBe(''); 91 expect(el.style.display).toBe('');
92 }); 92 });
93 }); 93 });
...@@ -96,14 +96,14 @@ describe('Routines', function() { ...@@ -96,14 +96,14 @@ describe('Routines', function() {
96 describe('enabled', function() { 96 describe('enabled', function() {
97 describe('with a truthy value', function() { 97 describe('with a truthy value', function() {
98 it('enables the element', function() { 98 it('enables the element', function() {
99 rivets.routines.enabled(el, true); 99 rivets.binders.enabled(el, true);
100 expect(el.disabled).toBe(false); 100 expect(el.disabled).toBe(false);
101 }); 101 });
102 }); 102 });
103 103
104 describe('with a falsey value', function() { 104 describe('with a falsey value', function() {
105 it('disables the element', function() { 105 it('disables the element', function() {
106 rivets.routines.enabled(el, false); 106 rivets.binders.enabled(el, false);
107 expect(el.disabled).toBe(true); 107 expect(el.disabled).toBe(true);
108 }); 108 });
109 }); 109 });
...@@ -112,14 +112,14 @@ describe('Routines', function() { ...@@ -112,14 +112,14 @@ describe('Routines', function() {
112 describe('disabled', function() { 112 describe('disabled', function() {
113 describe('with a truthy value', function() { 113 describe('with a truthy value', function() {
114 it('disables the element', function() { 114 it('disables the element', function() {
115 rivets.routines.disabled(el, true); 115 rivets.binders.disabled(el, true);
116 expect(el.disabled).toBe(true); 116 expect(el.disabled).toBe(true);
117 }); 117 });
118 }); 118 });
119 119
120 describe('with a falsey value', function() { 120 describe('with a falsey value', function() {
121 it('enables the element', function() { 121 it('enables the element', function() {
122 rivets.routines.disabled(el, false); 122 rivets.binders.disabled(el, false);
123 expect(el.disabled).toBe(false); 123 expect(el.disabled).toBe(false);
124 }); 124 });
125 }); 125 });
...@@ -128,14 +128,14 @@ describe('Routines', function() { ...@@ -128,14 +128,14 @@ describe('Routines', function() {
128 describe('checked', function() { 128 describe('checked', function() {
129 describe('with a truthy value', function() { 129 describe('with a truthy value', function() {
130 it('checks the element', function() { 130 it('checks the element', function() {
131 rivets.routines.checked(el, true); 131 rivets.binders.checked.routine(el, true);
132 expect(el.checked).toBe(true); 132 expect(el.checked).toBe(true);
133 }); 133 });
134 }); 134 });
135 135
136 describe('with a falsey value', function() { 136 describe('with a falsey value', function() {
137 it('unchecks the element', function() { 137 it('unchecks the element', function() {
138 rivets.routines.checked(el, false); 138 rivets.binders.checked.routine(el, false);
139 expect(el.checked).toBe(false); 139 expect(el.checked).toBe(false);
140 }); 140 });
141 }); 141 });
...@@ -144,14 +144,14 @@ describe('Routines', function() { ...@@ -144,14 +144,14 @@ describe('Routines', function() {
144 describe('unchecked', function() { 144 describe('unchecked', function() {
145 describe('with a truthy value', function() { 145 describe('with a truthy value', function() {
146 it('unchecks the element', function() { 146 it('unchecks the element', function() {
147 rivets.routines.unchecked(el, true); 147 rivets.binders.unchecked.routine(el, true);
148 expect(el.checked).toBe(false); 148 expect(el.checked).toBe(false);
149 }); 149 });
150 }); 150 });
151 151
152 describe('with a falsey value', function() { 152 describe('with a falsey value', function() {
153 it('checks the element', function() { 153 it('checks the element', function() {
154 rivets.routines.unchecked(el, false); 154 rivets.binders.unchecked.routine(el, false);
155 expect(el.checked).toBe(true); 155 expect(el.checked).toBe(true);
156 }); 156 });
157 }); 157 });
......