Debian Codegraph @

test/bindData.js

raw symbols SCIP
1 $(document).ready(function() {
2
3 module("view.stickit");
4
5 test('input:text', function() {
6
7 model.set({'water':'fountain'});
8 view.model = model;
9 view.templateId = 'jst1';
10 view.bindings = {
11 '#test1': {
12 observe: 'water'
13 }
14 };
15 $('#qunit-fixture').html(view.render().el);
16
17 equal(view.$('#test1').val(), 'fountain');
18
19 model.set('water', 'evian');
20 equal(view.$('#test1').val(), 'evian');
21
22 view.$('#test1').val('dasina').trigger('change');
23 equal(model.get('water'), 'dasina');
24 });
25
26 test('textarea', function() {
27
28 model.set({'water':'fountain'});
29 view.model = model;
30 view.templateId = 'jst2';
31 view.bindings = {
32 '#test2': {
33 observe: 'water'
34 }
35 };
36 $('#qunit-fixture').html(view.render().el);
37
38 equal(view.$('#test2').val(), 'fountain');
39
40 model.set('water', 'evian');
41 equal(view.$('#test2').val(), 'evian');
42
43 view.$('#test2').val('dasina').trigger('change');
44 equal(model.get('water'), 'dasina');
45 });
46
47 _([1,2]).each(function(subtest) {
48 var sel = '#test17-' + subtest;
49
50 test('contenteditable-' + subtest, function() {
51 model.set({'water':'<span>fountain</span>'});
52 view.model = model;
53 view.templateId = 'jst17';
54 view.bindings = {};
55 view.bindings[sel] = {
56 observe: 'water'
57 };
58 $('#qunit-fixture').html(view.render().el);
59
60 equal(view.$(sel).html(), '<span>fountain</span>');
61
62 model.set('water', '<span>evian</span>');
63 equal(view.$(sel).html(), '<span>evian</span>');
64
65 view.$(sel).html('<span>dasina</span>').trigger('change');
66 equal(model.get('water'), '<span>dasina</span>');
67 });
68
69 });
70
71 test('checkbox', function() {
72
73 model.set({'water':true});
74 view.model = model;
75 view.templateId = 'jst3';
76 view.bindings = {
77 '#test3': {
78 observe: 'water'
79 }
80 };
81 $('#qunit-fixture').html(view.render().el);
82
83 equal(view.$('#test3').prop('checked'), true);
84
85 model.set('water', false);
86 equal(view.$('#test3').prop('checked'), false);
87
88 view.$('#test3').prop('checked', true).trigger('change');
89 equal(model.get('water'), true);
90 });
91
92 test('radio', function() {
93
94 model.set({'water':'fountain'});
95 view.model = model;
96 view.templateId = 'jst4';
97 view.bindings = {
98 '.test4': {
99 observe: 'water'
100 }
101 };
102 $('#qunit-fixture').html(view.render().el);
103
104 equal(view.$('.test4:checked').val(), 'fountain');
105
106 model.set('water', 'evian');
107 equal(view.$('.test4:checked').val(), 'evian');
108
109 view.$('.test4[value=fountain]').prop('checked', true).trigger('change');
110 equal(model.get('water'), 'fountain');
111 });
112
113 test('div', function() {
114
115 model.set({'water':'fountain'});
116 view.model = model;
117 view.templateId = 'jst5';
118 view.bindings = {
119 '#test5': {
120 observe: 'water'
121 }
122 };
123 $('#qunit-fixture').html(view.render().el);
124
125 equal(view.$('#test5').text(), 'fountain');
126
127 model.set('water', 'evian');
128 equal(view.$('#test5').text(), 'evian');
129 });
130
131 test(':el selector', function() {
132
133 model.set({'water':'fountain'});
134 view.model = model;
135 view.templateId = 'jst5';
136 view.bindings = {
137 ':el': {
138 observe: 'water'
139 }
140 };
141 $('#qunit-fixture').html(view.render().el);
142
143 equal(view.$el.text(), 'fountain');
144
145 model.set('water', 'evian');
146 equal(view.$el.text(), 'evian');
147 });
148
149 test('bindings as a function', 2, function() {
150
151 model.set({'water':'fountain'});
152 view.model = model;
153 view.templateId = 'jst1';
154 view.bindings = function() {
155 return {'#test1':'water'};
156 };
157 $('#qunit-fixture').html(view.render().el);
158
159 equal(view.$('#test1').val(), 'fountain');
160 view.$('#test1').val('dasina').trigger('change');
161 equal(model.get('water'), 'dasina');
162 });
163
164 test('stickit (shorthand bindings)', function() {
165 model.set({'water':'fountain'});
166 view.model = model;
167 view.templateId = 'jst5';
168 view.bindings = {
169 '#test5': 'water'
170 };
171 $('#qunit-fixture').html(view.render().el);
172
173 equal(view.$('#test5').text(), 'fountain');
174
175 model.set('water', 'evian');
176
177 equal(view.$('#test5').text(), 'evian');
178
179 });
180
181 test('stickit (multiple models and bindings)', function() {
182
183 // Test sticking two times to two different models and configs.
184 var model1, model2, testView;
185
186 model1 = new (Backbone.Model)({id:1, water:'fountain', candy:'twix'});
187 model2 = new (Backbone.Model)({id:2, water:'evian', candy:'snickers'});
188
189 testView = new (Backbone.View.extend({
190 initialize: function() {
191 this.model = model1;
192 this.otherModel = model2;
193 },
194 bindings: {
195 '#test0-div': {
196 observe: 'water'
197 },
198 '#test0-textarea': {
199 observe: 'candy'
200 }
201 },
202 otherBindings: {
203 '#test0-span': {
204 observe: 'water'
205 },
206 '#test0-input': {
207 observe: 'candy'
208 }
209 },
210 render: function() {
211 var html = document.getElementById('jst0').innerHTML;
212 this.$el.html(_.template(html)());
213 this.stickit();
214 this.stickit(this.otherModel, this.otherBindings);
215 return this;
216 }
217 }))();
218
219 $('#qunit-fixture').html(testView.render().el);
220
221 equal(testView.$('#test0-div').text(), 'fountain');
222 equal(testView.$('#test0-textarea').val(), 'twix');
223 equal(testView.$('#test0-span').text(), 'evian');
224 equal(testView.$('#test0-input').val(), 'snickers');
225
226 model1.set({water:'dasina', candy: 'mounds'});
227 model2.set({water:'poland springs', candy: 'almond joy'});
228
229 equal(testView.$('#test0-div').text(), 'dasina');
230 equal(testView.$('#test0-textarea').val(), 'mounds');
231 equal(testView.$('#test0-span').text(), 'poland springs');
232 equal(testView.$('#test0-input').val(), 'almond joy');
233
234 testView.$('#test0-textarea').val('kit kat').trigger('change');
235 testView.$('#test0-input').val('butterfinger').trigger('change');
236
237 equal(model1.get('candy'), 'kit kat');
238 equal(model2.get('candy'), 'butterfinger');
239
240 testView.remove();
241 });
242
243 test('stickit (existing events property as hash with multiple models and bindings)', function() {
244
245 var model1, model2, testView;
246
247 model1 = new (Backbone.Model)({id:1, candy:'twix' });
248 model2 = new (Backbone.Model)({id:2, candy:'snickers'});
249
250 testView = new (Backbone.View.extend({
251
252 initialize: function() {
253 this.model = model1;
254 this.otherModel = model2;
255 },
256
257 events: {
258 click: 'handleClick'
259 },
260
261 bindings: {
262 '#test0-textarea': 'candy'
263 },
264
265 otherBindings: {
266 '#test0-input': 'candy'
267 },
268
269 render: function() {
270 var html = document.getElementById('jst0').innerHTML;
271 this.$el.html(_.template(html)());
272 this.stickit();
273 this.stickit(this.otherModel, this.otherBindings);
274 return this;
275 },
276
277 handleClick: function() {
278 this.clickHandled = true;
279 }
280
281 }))();
282
283 $('#qunit-fixture').html(testView.render().el);
284
285 testView.$('#test0-textarea').val('kit kat').trigger('change');
286 testView.$('#test0-input').val('butterfinger').trigger('change');
287
288 equal(model1.get('candy'), 'kit kat');
289 equal(model2.get('candy'), 'butterfinger');
290
291 testView.$el.trigger('click');
292
293 equal(testView.clickHandled, true);
294
295 // Remove the view which should unbind the event handlers.
296 testView.remove();
297
298 testView.$('#test0-textarea').val('mounds').trigger('change');
299 testView.$('#test0-input').val('skittles').trigger('change');
300
301 equal(model1.get('candy'), 'kit kat');
302 equal(model2.get('candy'), 'butterfinger');
303 });
304
305 test('stickit (existing events property as function with multiple models and bindings)', function() {
306
307 var model1, model2, testView;
308
309 model1 = new (Backbone.Model)({id:1, candy:'twix' });
310 model2 = new (Backbone.Model)({id:2, candy:'snickers'});
311
312 testView = new (Backbone.View.extend({
313
314 initialize: function() {
315 this.model = model1;
316 this.otherModel = model2;
317 },
318
319 events: function() {
320 return {
321 click: this.clickHandled
322 };
323 },
324
325 clickHandled: function() {
326 this.clickHandled = true;
327 },
328
329 bindings: {
330 '#test0-textarea': 'candy'
331 },
332
333 otherBindings: {
334 '#test0-input': 'candy'
335 },
336
337 render: function() {
338 var html = document.getElementById('jst0').innerHTML;
339 this.$el.html(_.template(html)());
340 this.stickit();
341 this.stickit(this.otherModel, this.otherBindings);
342 return this;
343 }
344
345 }))();
346
347 $('#qunit-fixture').html(testView.render().el);
348
349 testView.$('#test0-textarea').val('kit kat').trigger('change');
350 testView.$('#test0-input').val('butterfinger').trigger('change');
351
352 equal(model1.get('candy'), 'kit kat');
353 equal(model2.get('candy'), 'butterfinger');
354
355 testView.$el.trigger('click');
356
357 equal(testView.clickHandled, true);
358
359 testView.remove();
360 });
361
362 test('bindings:setOptions', function() {
363
364 model.set({'water':'fountain'});
365 view.model = model;
366 view.templateId = 'jst1';
367 view.bindings = {
368 '#test1': {
369 observe: 'water',
370 setOptions: {silent:true}
371 }
372 };
373 $('#qunit-fixture').html(view.render().el);
374
375 equal(view.$('#test1').val(), 'fountain');
376
377 view.$('#test1').val('dasina').trigger('change');
378 equal(model.get('water'), 'dasina');
379 equal(model.changedAttributes().water, 'dasina');
380 });
381
382 test('bindings:updateMethod', function() {
383
384 model.set({'water':'<a href="www.test.com">river</a>'});
385 view.model = model;
386 view.templateId = 'jst5';
387 view.bindings = {
388 '#test5': {
389 observe: 'water',
390 updateMethod: 'html'
391 }
392 };
393 $('#qunit-fixture').html(view.render().el);
394
395 equal(view.$('#test5').text(), 'river');
396 });
397
398 test('bindings:escape', function() {
399
400 model.set({'water':'<a href="www.test.com">river</a>'});
401 view.model = model;
402 view.templateId = 'jst5';
403 view.bindings = {
404 '#test5': {
405 observe: 'water',
406 updateMethod: 'html',
407 escape: true
408 }
409 };
410
411 $('#qunit-fixture').html(view.render().el);
412
413 equal(view.$('#test5').text(), '<a href="www.test.com">river</a>');
414 });
415
416 test('bindings:onSet/onGet', 6, function() {
417
418 model.set({'water':'_fountain'});
419 view.model = model;
420 view.templateId = 'jst1';
421 view.bindings = {
422 '#test1': {
423 observe: 'water',
424 onGet: function(val, options) {
425 equal(val, this.model.get('water'));
426 equal(options.observe, 'water');
427 return val.substring(1);
428 },
429 onSet: function(val, options) {
430 equal(val, view.$('#test1').val());
431 equal(options.observe, 'water');
432 return '_' + val;
433 }
434 }
435 };
436
437 $('#qunit-fixture').html(view.render().el);
438
439 equal(view.$('#test1').val(), 'fountain');
440 view.$('#test1').val('evian').trigger('change');
441 equal(model.get('water'), '_evian');
442 });
443
444 test('bindings:sanitization', 7, function() {
445
446 model.set({'duration':null});
447 view.model = model;
448 view.templateId = 'jst1';
449 view.bindings = {
450 '#test1': {
451 observe: 'duration',
452 onGet: function(val, options) {
453 equal(val, null);
454 equal(options.observe, 'duration');
455 return val;
456 },
457 onSet: function(val, options) {
458 equal(parseInt(val, 10), 30);
459 equal(val, view.$('#test1').val());
460 equal(options.observe, 'duration');
461 return null;
462 }
463 }
464 };
465
466 $('#qunit-fixture').html(view.render().el);
467
468 equal(view.$('#test1').val(), '');
469 view.$('#test1').val(30).trigger('change');
470 equal(model.get('duration'), null);
471 });
472
473 test('bindings:onSet (returning array of observed values)', 2, function() {
474
475 model.set({'water':'fountain', 'candy':'skittles'});
476 view.model = model;
477 view.templateId = 'jst1';
478 view.bindings = {
479 '#test1': {
480 observe: ['water', 'candy'],
481 onGet: function(val, options) {
482 return model.get(options[0]) + '-' + model.get(options[1]);
483 },
484 onSet: function(val, options) {
485 return val.split('-');
486 }
487 }
488 };
489
490 $('#qunit-fixture').html(view.render().el);
491
492 view.$('#test1').val('evian-kitkat').trigger('change');
493 equal(model.get('water'), 'evian');
494 equal(model.get('candy'), 'kitkat');
495 });
496
497 test('bindings:set', 5, function() {
498
499 model.set({'water':'fountain'});
500 view.model = model;
501 view.templateId = 'jst1';
502 view.bindings = {
503 '#test1': {
504 observe: 'water',
505 set: function(attr, val, options, config) {
506 equal(attr, 'water');
507 equal(val, 'evian');
508 equal(options.stickitChange.observe, attr);
509 equal(config.observe, attr);
510 model.set(attr, val);
511 }
512 }
513 };
514
515 $('#qunit-fixture').html(view.render().el);
516
517 view.$('#test1').val('evian').trigger('change');
518 equal(model.get('water'), 'evian');
519 });
520
521 test('bindings:afterUpdate', 14, function() {
522
523 model.set({'water':'fountain', 'candy':true});
524 view.model = model;
525 view.templateId = 'jst15';
526 view.bindings = {
527 '#test15-1': {
528 observe: 'water',
529 afterUpdate: function($el, val, options) {
530 equal($el.text(), model.get('water'));
531 equal(val, 'evian');
532 equal(options.observe, 'water');
533 }
534 },
535 '#test15-2': {
536 observe: 'water',
537 afterUpdate: function($el, val, options) {
538 equal($el.val(), model.get('water'));
539 equal(val, 'evian');
540 equal(options.observe, 'water');
541 }
542 },
543 '#test15-3': {
544 observe: 'candy',
545 afterUpdate: function($el, val, options) {
546 equal(val, false);
547 equal(options.observe, 'candy');
548 }
549 },
550 '.test15-4': {
551 observe: 'water',
552 afterUpdate: function($el, val, options) {
553 equal(val, 'evian');
554 equal(options.observe, 'water');
555 }
556 },
557 '#test15-6': {
558 observe: 'water',
559 afterUpdate: function($el, val, options) {
560 equal(val, 'evian');
561 equal(options.observe, 'water');
562 }
563 },
564 '#test15-7': {
565 observe: 'water',
566 selectOptions: {
567 collection: function() { return [{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]; },
568 labelPath: 'name',
569 valuePath: 'name'
570 },
571 afterUpdate: function($el, val, options) {
572 equal(val, 'evian');
573 equal(options.observe, 'water');
574 }
575 }
576 };
577 $('#qunit-fixture').html(view.render().el);
578
579 model.set('water', 'evian');
580 model.set('candy', false);
581 });
582
583 test('bindings:selectOptions', 7, function() {
584
585 model.set({'water':'fountain'});
586 view.model = model;
587 view.templateId = 'jst8';
588 view.bindings = {
589 '#test8': {
590 observe: 'water',
591 selectOptions: {
592 collection: function($el, options) {
593 ok($el.is('select'));
594 equal(options.observe, 'water');
595 return [{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}];
596 },
597 labelPath: 'name',
598 valuePath: 'name'
599 }
600 }
601 };
602
603 $('#qunit-fixture').html(view.render().el);
604
605 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'fountain');
606
607 model.set('water', 'evian');
608 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
609
610 view.$('#test8 option').eq(2).prop('selected', true).trigger('change');
611 equal(model.get('water'), 'dasina');
612 });
613
614 test('bindings:selectOptions:defaultOption', 8, function() {
615
616 model.set({'water':null});
617 view.model = model;
618 view.templateId = 'jst8';
619 view.bindings = {
620 '#test8': {
621 observe: 'water',
622 selectOptions: {
623 collection: function($el, options) {
624 ok($el.is('select'));
625 equal(options.observe, 'water');
626 return [{id:1,type:{name:'fountain'}}, {id:2,type:{name:'evian'}}, {id:3,type:{name:'dasina'}}];
627 },
628 defaultOption: {
629 label: 'Choose one...',
630 value: null
631 },
632 labelPath: 'type.name',
633 valuePath: 'type.name'
634 }
635 }
636 };
637
638 $('#qunit-fixture').html(view.render().el);
639
640 equal(view.$('#test8 option').eq(0).text(), 'Choose one...');
641 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), null);
642
643 model.set('water', 'evian');
644 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
645
646 view.$('#test8 option').eq(3).prop('selected', true).trigger('change');
647 equal(model.get('water'), 'dasina');
648 });
649
650 test('bindings:selectOptions:defaultOption (options is a method)', 8, function() {
651
652 model.set({'water':null});
653 view.model = model;
654 view.templateId = 'jst8';
655 view.bindings = {
656 '#test8': {
657 observe: 'water',
658 selectOptions: {
659 collection: function($el, options) {
660 ok($el.is('select'));
661 equal(options.observe, 'water');
662 return [{id:1,type:{name:'fountain'}}, {id:2,type:{name:'evian'}}, {id:3,type:{name:'dasina'}}];
663 },
664 defaultOption: function(){
665 return {label: 'Choose dynamic...',
666 value: null};
667 },
668 labelPath: 'type.name',
669 valuePath: 'type.name'
670 }
671 }
672 };
673
674 $('#qunit-fixture').html(view.render().el);
675
676 equal(view.$('#test8 option').eq(0).text(), 'Choose dynamic...');
677 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), null);
678
679 model.set('water', 'evian');
680 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
681
682 view.$('#test8 option').eq(3).prop('selected', true).trigger('change');
683 equal(model.get('water'), 'dasina');
684 });
685
686 test('bindings:selectOptions:defaultOption (options is disabled)', 9, function() {
687
688 model.set({'water':null});
689 view.model = model;
690 view.templateId = 'jst8';
691 view.bindings = {
692 '#test8': {
693 observe: 'water',
694 selectOptions: {
695 collection: function($el, options) {
696 ok($el.is('select'));
697 equal(options.observe, 'water');
698 return [{id:1,type:{name:'fountain'}}, {id:2,type:{name:'evian'}}, {id:3,type:{name:'dasina'}}];
699 },
700 defaultOption: {
701 label: 'Choose one...',
702 value: null,
703 disabled: true
704 },
705 labelPath: 'type.name',
706 valuePath: 'type.name'
707 }
708 }
709 };
710
711 $('#qunit-fixture').html(view.render().el);
712
713 equal(view.$('#test8 option').eq(0).text(), 'Choose one...');
714 ok(view.$('#test8 option').eq(0).prop('disabled'));
715
716 equal(model.get('water'), null);
717
718 model.set('water', 'evian');
719 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
720
721 // We can force the selection of disabled options
722 view.$('#test8 option').eq(0).prop('selected', true).trigger('change');
723 equal(model.get('water'), null);
724 });
725
726 test('bindings:selectOptions:defaultOption:OptGroups', 8, function() {
727
728 model.set({'water':null});
729 view.model = model;
730 view.templateId = 'jst8';
731 view.bindings = {
732 '#test8': {
733 observe: 'water',
734 selectOptions: {
735 collection: function($el, options) {
736 ok($el.is('select'));
737 equal(options.observe, 'water');
738 return {
739 'opt_labels': ['Types'],
740 'Types': [{id:1,type:{name:'fountain'}}, {id:2,type:{name:'evian'}}, {id:3,type:{name:'dasina'}}]
741 };
742 },
743 defaultOption: {
744 label: 'Choose one...',
745 value: null
746 },
747 labelPath: 'type.name',
748 valuePath: 'type.name'
749 }
750 }
751 };
752
753 $('#qunit-fixture').html(view.render().el);
754
755 equal(view.$('#test8 > option').eq(0).text(), 'Choose one...');
756 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), null);
757
758 model.set('water', 'evian');
759 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
760
761 view.$('#test8 option').eq(3).prop('selected', true).trigger('change');
762 equal(model.get('water'), 'dasina');
763 });
764
765 test('bindings:selectOptions (disabled options)' , function() {
766
767 model.set({'water':'fountain'});
768 view.model = model;
769 view.templateId = 'jst8';
770 view.bindings = {
771 '#test8': {
772 observe: 'water',
773 selectOptions: {
774 collection: [{id:1,name:'fountain'}, {id:2,name:'evian',disabled:true}, {id:3,name:'dasina'}],
775 labelPath: 'name',
776 valuePath: 'id'
777 }
778 }
779 };
780
781 $('#qunit-fixture').html(view.render().el);
782
783 equal(view.$('#test8 option').eq(0).prop('disabled'), false);
784 equal(view.$('#test8 option').eq(1).prop('disabled'), true);
785 equal(view.$('#test8 option').eq(2).prop('disabled'), false);
786 });
787
788 test('bindings:selectOptions (pre-rendered)', 3, function() {
789
790 // Note that we're working with strings and not numeric values here - the pre-rendered <select>
791 // handling is limited to strings unless data-stickit-bind-val is specified for each <option>
792
793 model.set({'water':'1'});
794 view.model = model;
795 view.templateId = 'jst21';
796 view.bindings = {
797 '#test21': {
798 observe: 'water'
799 }
800 };
801
802 $('#qunit-fixture').html(view.render().el);
803
804 strictEqual(getSelectedOption(view.$('#test21')).data('stickit-bind-val'), '1');
805
806 model.set('water', '2');
807 strictEqual(getSelectedOption(view.$('#test21')).data('stickit-bind-val'), '2');
808
809 view.$('#test21 option').eq(2).prop('selected', true).trigger('change');
810 strictEqual(model.get('water'), '3');
811 });
812
813 test('bindings:selectOptions (pre-rendered, with data-stickit-bind-val)', function() {
814
815 // Here we're testing that numeric values can be bound via data-stickit-bind-val
816
817 model.set({'water': 1});
818 view.model = model;
819 view.templateId = 'jst26';
820 view.bindings = {
821 '#test26': {
822 observe: 'water'
823 }
824 };
825
826 $('#qunit-fixture').html(view.render().el);
827
828 strictEqual(getSelectedOption(view.$('#test26')).data('stickit-bind-val'), 1);
829
830 model.set('water', 2);
831 strictEqual(getSelectedOption(view.$('#test26')).data('stickit-bind-val'), 2);
832
833 view.$('#test26 option:contains("dasina")').prop('selected', true).trigger('change');
834 strictEqual(model.get('water'), 3);
835
836 view.$('#test26 option:contains("foutain")').prop('selected', true).trigger('change');
837 strictEqual(model.get('water'), 0);
838 });
839
840 test('bindings:selectOptions (Backbone.Collection)', function() {
841
842 var collection = new Backbone.Collection([{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]);
843 model.set({'water':'fountain'});
844 view.model = model;
845 view.templateId = 'jst8';
846 view.bindings = {
847 '#test8': {
848 observe: 'water',
849 selectOptions: {
850 collection: function() { return collection; },
851 labelPath: 'name',
852 valuePath: 'name'
853 }
854 }
855 };
856
857 $('#qunit-fixture').html(view.render().el);
858
859 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'fountain');
860
861 model.set('water', 'evian');
862 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
863
864 view.$('#test8 option').eq(2).prop('selected', true).trigger('change');
865 equal(model.get('water'), 'dasina');
866 });
867
868 test('bindings:selectOptions (Backbone.Collection that changes)', function() {
869
870 var collection = new Backbone.Collection([{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]);
871 model.set({'water':'fountain'});
872 view.model = model;
873 view.templateId = 'jst8';
874 view.bindings = {
875 '#test8': {
876 observe: 'water',
877 selectOptions: {
878 collection: function() { return collection; },
879 labelPath: 'name',
880 valuePath: 'name'
881 }
882 }
883 };
884
885 $('#qunit-fixture').html(view.render().el);
886
887 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'fountain');
888
889 model.set('water', 'evian');
890 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
891
892 view.$('#test8 option').eq(2).prop('selected', true).trigger('change');
893 equal(model.get('water'), 'dasina');
894
895 // Test that the select options are auto-updated
896 collection.add({id:4,name:'buxton'});
897 equal(view.$('#test8 option').eq(3).data('stickit-bind-val'), 'buxton');
898
899 var modelEvents = ['stickit:unstuck', 'stickit:selectRefresh'];
900 var collectionEvents = ['stickit:selectRefresh', 'add', 'remove', 'reset', 'sort'];
901
902 // Test the number of event listeners set against the model and collection
903 equal(_.filter(model._events, function(event, key) {
904 return (event.length === 1 && _.contains(modelEvents, key));
905 }).length, modelEvents.length);
906
907 equal(_.filter(collection._events, function(event, key) {
908 return (event.length === 1 && _.contains(collectionEvents, key));
909 }).length, collectionEvents.length);
910
911 collection.remove(2);
912 equal(view.$('#test8 option').length, collection.length);
913
914 collection.reset();
915 equal(view.$('#test8 option').length, collection.length);
916
917 // Test the number of event listeners set against the model and collection after changes to the collection
918 equal(_.filter(model._events, function(event, key) {
919 return (event.length === 1 && _.contains(modelEvents, key));
920 }).length, modelEvents.length);
921
922 equal(_.filter(collection._events, function(event, key) {
923 return (event.length === 1 && _.contains(collectionEvents, key));
924 }).length, collectionEvents.length);
925
926 view.unstickit();
927
928 // Test that the select options are no longer updated
929 collection.add([{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]);
930 notEqual(view.$('#test8 option').length, collection.length);
931
932 // Test that all event listeners have been removed after unstickit
933 ok(_.isEmpty(model._events));
934 ok(_.isEmpty(collection._events));
935 });
936
937 test('bindings:selectOptions (collection path relative to `this`)', function() {
938
939 view.collection = new Backbone.Collection([{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]);
940 model.set({'water':'fountain'});
941 view.model = model;
942 view.templateId = 'jst8';
943 view.bindings = {
944 '#test8': {
945 observe: 'water',
946 selectOptions: {
947 collection: 'this.collection',
948 labelPath: 'name',
949 valuePath: 'name'
950 }
951 }
952 };
953
954 $('#qunit-fixture').html(view.render().el);
955
956 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'fountain');
957
958 model.set('water', 'evian');
959 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
960
961 view.$('#test8 option').eq(2).prop('selected', true).trigger('change');
962 equal(model.get('water'), 'dasina');
963 });
964
965 test('bindings:selectOptions (empty valuePath)', function() {
966
967 model.set({'water':{id:1, name:'fountain'}});
968 window.test = {
969 collection: [{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]
970 };
971 view.model = model;
972 view.templateId = 'jst8';
973 view.bindings = {
974 '#test8': {
975 observe: 'water',
976 selectOptions: {
977 collection: 'window.test.collection',
978 labelPath: 'name'
979 }
980 }
981 };
982 $('#qunit-fixture').html(view.render().el);
983
984 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val').id, 1);
985
986 model.set('water', {id:2, name:'evian'});
987 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val').id, 2);
988
989 view.$('#test8 option').eq(2).prop('selected', true).trigger('change');
990 equal(model.get('water').id, 3);
991 });
992
993 test('bindings:selectOptions (empty string label)', function() {
994
995 model.set({'water':'session'});
996 view.model = model;
997 view.templateId = 'jst8';
998 view.bindings = {
999 '#test8': {
1000 observe: 'water',
1001 selectOptions: {
1002 collection: function() {
1003 return [{label:'c',value:''}, {label:'s',value:'session'}];
1004 },
1005 labelPath: "label",
1006 valuePath: "value"
1007 }
1008 }
1009 };
1010
1011 $('#qunit-fixture').html(view.render().el);
1012
1013 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'session');
1014 equal(view.$('#test8 option').eq(0).data('stickit-bind-val'), '');
1015
1016 model.set('water', '');
1017 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), '');
1018 });
1019
1020 test('bindings:selectOptions (default labelPath/valuePath)', function() {
1021
1022 model.set({'water':'evian'});
1023 view.model = model;
1024 view.templateId = 'jst8';
1025 view.bindings = {
1026 '#test8': {
1027 observe: 'water',
1028 selectOptions: {
1029 collection: function() {
1030 return [{label:'c',value:'fountain'}, {label:'s',value:'evian'}];
1031 }
1032 }
1033 }
1034 };
1035
1036 $('#qunit-fixture').html(view.render().el);
1037
1038 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'evian');
1039
1040 model.set('water', 'fountain');
1041 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'fountain');
1042 });
1043
1044 test('bindings:selectOptions (collection defined as value/label map)', function() {
1045
1046 model.set({'sound':'moo'});
1047 view.model = model;
1048 view.templateId = 'jst8';
1049 view.bindings = {
1050 '#test8': {
1051 observe: 'sound',
1052 selectOptions: {
1053 collection: {
1054 moo: 'cow',
1055 baa: 'sheep',
1056 oink: 'pig'
1057 }
1058 }
1059 }
1060 };
1061
1062 $('#qunit-fixture').html(view.render().el);
1063
1064 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'moo');
1065
1066 // Options are sorted alphabetically by label
1067 equal(view.$('#test8 option:eq(0)').data('stickit-bind-val'), 'moo');
1068 equal(view.$('#test8 option:eq(1)').data('stickit-bind-val'), 'oink');
1069 equal(view.$('#test8 option:eq(2)').data('stickit-bind-val'), 'baa');
1070 });
1071
1072 test('bindings:selectOptions (collection defined as value/label map, sorted by value)', function() {
1073
1074 model.set({'sound':'moo'});
1075 view.model = model;
1076 view.templateId = 'jst8';
1077 view.bindings = {
1078 '#test8': {
1079 observe: 'sound',
1080 selectOptions: {
1081 collection: {
1082 moo: 'cow',
1083 baa: 'sheep',
1084 oink: 'pig'
1085 },
1086 comparator: function(sound) { return sound.value; }
1087 }
1088 }
1089 };
1090
1091 $('#qunit-fixture').html(view.render().el);
1092
1093 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 'moo');
1094
1095 // Options are sorted alphabetically by value
1096 equal(view.$('#test8 option:eq(0)').data('stickit-bind-val'), 'baa');
1097 equal(view.$('#test8 option:eq(1)').data('stickit-bind-val'), 'moo');
1098 equal(view.$('#test8 option:eq(2)').data('stickit-bind-val'), 'oink');
1099 });
1100
1101 test('bindings:selectOptions (multi-select without valuePath)', function() {
1102
1103 var collection = [{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}, {id:4,name:'aquafina'}];
1104
1105 model.set({'water': [{id:1,name:'fountain'}, {id:3,name:'dasina'}] });
1106 view.model = model;
1107 view.templateId = 'jst16';
1108 view.bindings = {
1109 '#test16': {
1110 observe: 'water',
1111 selectOptions: {
1112 collection: function() { return collection; },
1113 labelPath: 'name'
1114 }
1115 }
1116 };
1117
1118 $('#qunit-fixture').html(view.render().el);
1119
1120 equal(getSelectedOption(view.$('#test16')).eq(0).data('stickit-bind-val').name, 'fountain');
1121 equal(getSelectedOption(view.$('#test16')).eq(1).data('stickit-bind-val').name, 'dasina');
1122
1123 var field = _.clone(model.get('water'));
1124 field.push({id:2,name:'evian'});
1125
1126 model.set({'water':field});
1127 equal(getSelectedOption(view.$('#test16')).eq(1).data('stickit-bind-val').name, 'evian');
1128
1129 view.$('#test16 option').eq(3).prop('selected', true).trigger('change');
1130
1131 equal(model.get('water').length, 4);
1132
1133 });
1134
1135 test('bindings:selectOptions (multi-select with valuePath)', function() {
1136
1137 var collection = [{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}, {id:4,name:'aquafina'}];
1138
1139 model.set({'water': [1, 3]});
1140 view.model = model;
1141 view.templateId = 'jst16';
1142 view.bindings = {
1143 '#test16': {
1144 observe: 'water',
1145 selectOptions: {
1146 collection: function() { return collection; },
1147 labelPath: 'name',
1148 valuePath: 'id'
1149 }
1150 }
1151 };
1152
1153 $('#qunit-fixture').html(view.render().el);
1154
1155 equal(getSelectedOption(view.$('#test16')).eq(0).data('stickit-bind-val'), 1);
1156 equal(getSelectedOption(view.$('#test16')).eq(1).data('stickit-bind-val'), 3);
1157
1158 var field = _.clone(model.get('water'));
1159 field.push(2);
1160
1161 model.set({'water':field});
1162 equal(getSelectedOption(view.$('#test16')).eq(1).data('stickit-bind-val'), 2);
1163
1164 view.$('#test16 option').eq(3).prop('selected', true).trigger('change');
1165
1166 equal(model.get('water').length, 4);
1167
1168 });
1169
1170 test('bindings:selectOptions (pre-rendered multi-select)', function() {
1171
1172 model.set({'water': ['1', '3']});
1173 view.model = model;
1174 view.templateId = 'jst23';
1175 view.bindings = {
1176 '#test23': {
1177 observe: 'water'
1178 }
1179 };
1180
1181 $('#qunit-fixture').html(view.render().el);
1182
1183 equal(getSelectedOption(view.$('#test23')).eq(0).data('stickit-bind-val'), '1');
1184 equal(getSelectedOption(view.$('#test23')).eq(1).data('stickit-bind-val'), '3');
1185
1186 var field = _.clone(model.get('water'));
1187 field.push('2');
1188
1189 model.set({'water':field});
1190 equal(getSelectedOption(view.$('#test23')).eq(1).data('stickit-bind-val'), '2');
1191
1192 view.$('#test23 option').eq(3).prop('selected', true).trigger('change');
1193
1194 equal(model.get('water').length, '4');
1195
1196 });
1197
1198 test('bindings:selectOptions (multi-select with onGet/onSet)', function() {
1199
1200 var collection = [{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}, {id:4,name:'aquafina'}];
1201
1202 model.set({'water':'1-3'});
1203 view.model = model;
1204 view.templateId = 'jst16';
1205 view.bindings = {
1206 '#test16': {
1207 observe: 'water',
1208 onGet: function(val) {
1209 return _.map(val.split('-'), function(id) {return Number(id);});
1210 },
1211 onSet: function(vals) {
1212 return vals.join('-');
1213 },
1214 selectOptions: {
1215 collection: function() { return collection; },
1216 labelPath: 'name',
1217 valuePath: 'id'
1218 }
1219 }
1220 };
1221
1222 $('#qunit-fixture').html(view.render().el);
1223
1224 equal(getSelectedOption(view.$('#test16')).eq(0).data('stickit-bind-val'), 1);
1225 equal(getSelectedOption(view.$('#test16')).eq(1).data('stickit-bind-val'), 3);
1226
1227 var field = _.clone(model.get('water'));
1228 field += '-2';
1229
1230 model.set({'water':field});
1231 equal(getSelectedOption(view.$('#test16')).eq(1).data('stickit-bind-val'), 2);
1232
1233 view.$('#test16 option').eq(3).prop('selected', true).trigger('change');
1234
1235 equal(model.get('water'), '1-2-3-4');
1236
1237 });
1238
1239 test('bindings:selectOptions (optgroup)', function() {
1240
1241 model.set({'character':3});
1242 view.model = model;
1243 view.templateId = 'jst8';
1244 view.bindings = {
1245 '#test8': {
1246 observe: 'character',
1247 selectOptions: {
1248 collection: function() {
1249 return {
1250 'opt_labels': ['Looney Tunes', 'Three Stooges'],
1251 'Looney Tunes': [{id: 1, name: 'Bugs Bunny'}, {id: 2, name: 'Donald Duck'}],
1252 'Three Stooges': [{id: 3, name : 'moe'}, {id: 4, name : 'larry'}, {id: 5, name : 'curly'}]
1253 };
1254 },
1255 labelPath: 'name',
1256 valuePath: 'id'
1257 }
1258 }
1259 };
1260
1261 $('#qunit-fixture').html(view.render().el);
1262
1263 equal(getSelectedOption(view.$('#test8')).parent().is('optgroup'), true);
1264 equal(getSelectedOption(view.$('#test8')).parent().attr('label'), 'Three Stooges');
1265 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 3);
1266
1267 model.set({'character':2});
1268 equal(getSelectedOption(view.$('#test8')).data('stickit-bind-val'), 2);
1269
1270 view.$('#test8 option').eq(3).prop('selected', true).trigger('change');
1271 equal(model.get('character'), 4);
1272 });
1273
1274 test('bindings:selectOptions (pre-rendered optgroup)', function() {
1275
1276 model.set({'character':'3'});
1277 view.model = model;
1278 view.templateId = 'jst22';
1279 view.bindings = {
1280 '#test22': {
1281 observe: 'character'
1282 }
1283 };
1284
1285 $('#qunit-fixture').html(view.render().el);
1286
1287 equal(getSelectedOption(view.$('#test22')).parent().is('optgroup'), true);
1288 equal(getSelectedOption(view.$('#test22')).parent().attr('label'), 'Three Stooges');
1289 equal(getSelectedOption(view.$('#test22')).data('stickit-bind-val'), '3');
1290
1291 model.set({'character':'2'});
1292 equal(getSelectedOption(view.$('#test22')).data('stickit-bind-val'), '2');
1293
1294 view.$('#test22 option').eq(3).prop('selected', true).trigger('change');
1295 equal(model.get('character'), '4');
1296 });
1297
1298 test('bindings:selectOptions (pre-rendered optgroup with extra option)', function() {
1299
1300 model.set({'character':'3'});
1301 view.model = model;
1302 view.templateId = 'jst24';
1303 view.bindings = {
1304 '#test24': {
1305 observe: 'character'
1306 }
1307 };
1308
1309 $('#qunit-fixture').html(view.render().el);
1310
1311 equal(getSelectedOption(view.$('#test24')).parent().is('optgroup'), true);
1312 equal(getSelectedOption(view.$('#test24')).parent().attr('label'), 'Three Stooges');
1313 equal(getSelectedOption(view.$('#test24')).data('stickit-bind-val'), '3');
1314
1315 model.set({'character':'0'});
1316 equal(getSelectedOption(view.$('#test24')).data('stickit-bind-val'), '0');
1317
1318 view.$('#test24 option').eq(4).prop('selected', true).trigger('change');
1319 equal(model.get('character'), '4');
1320 });
1321
1322 test('bindings:attributes:name', function() {
1323
1324 model.set({'water':'fountain'});
1325 view.model = model;
1326 view.templateId = 'jst5';
1327 view.bindings = {
1328 '#test5': {
1329 observe: 'water',
1330 attributes: [{
1331 name: 'data-name'
1332
1333 }]
1334 }
1335 };
1336
1337 $('#qunit-fixture').html(view.render().el);
1338
1339 equal(view.$('#test5').attr('data-name'), 'fountain');
1340
1341 model.set('water', 'evian');
1342 equal(view.$('#test5').attr('data-name'), 'evian');
1343 });
1344
1345 test('bindings:attributes:name:class', function() {
1346
1347 model.set({'water':'fountain'});
1348 view.model = model;
1349 view.templateId = 'jst9';
1350 view.bindings = {
1351 '#test9': {
1352 observe: 'water',
1353 attributes: [{
1354 name: 'class'
1355 }]
1356 }
1357 };
1358
1359 $('#qunit-fixture').html(view.render().el);
1360
1361 ok(view.$('#test9').hasClass('test') && view.$('#test9').hasClass('fountain'));
1362
1363 model.set('water', 'evian');
1364 ok(view.$('#test9').hasClass('test') && view.$('#test9').hasClass('evian'));
1365 });
1366
1367 test('bindings:attributes:onGet', function() {
1368
1369 model.set({'water':'fountain'});
1370 view.model = model;
1371 view.templateId = 'jst5';
1372 view.bindings = {
1373 '#test5': {
1374 observe: 'water',
1375 attributes: [{
1376 name: 'data-name',
1377 onGet: function(val, options) { return '_' + val + '_' + options.observe; }
1378 }]
1379 }
1380 };
1381
1382 $('#qunit-fixture').html(view.render().el);
1383
1384 equal(view.$('#test5').attr('data-name'), '_fountain_water');
1385
1386 model.set('water', 'evian');
1387 equal(view.$('#test5').attr('data-name'), '_evian_water');
1388 });
1389
1390 test('bindings:attributes:onGet (string)', function() {
1391
1392 model.set({'water':'fountain'});
1393 view.model = model;
1394 view.templateId = 'jst5';
1395 view.onGetByString = function(val, options) { return '_' + val + '_' + options.observe; };
1396 view.bindings = {
1397 '#test5': {
1398 observe: 'water',
1399 attributes: [{
1400 name: 'data-name',
1401 onGet: 'onGetByString'
1402 }]
1403 }
1404 };
1405
1406 $('#qunit-fixture').html(view.render().el);
1407
1408 equal(view.$('#test5').attr('data-name'), '_fountain_water');
1409
1410 model.set('water', 'evian');
1411 equal(view.$('#test5').attr('data-name'), '_evian_water');
1412 });
1413
1414 test('bindings:attributes:observe', function() {
1415
1416 model.set({'water':'fountain', 'candy':'twix'});
1417 view.model = model;
1418 view.templateId = 'jst5';
1419 view.bindings = {
1420 '#test5': {
1421 attributes: [{
1422 name: 'data-name',
1423 observe: 'candy',
1424 onGet: function(val) {
1425 equal(val, this.model.get('candy'));
1426 return this.model.get('water') + '-' + this.model.get('candy');
1427 }
1428 }]
1429 }
1430 };
1431
1432 $('#qunit-fixture').html(view.render().el);
1433
1434 equal(view.$('#test5').attr('data-name'), 'fountain-twix');
1435
1436 model.set({'water':'evian', 'candy':'snickers'});
1437 equal(view.$('#test5').attr('data-name'), 'evian-snickers');
1438 });
1439
1440 test('bindings:attributes:observe (array)', 11, function() {
1441
1442 model.set({'water':'fountain', 'candy':'twix'});
1443 view.model = model;
1444 view.templateId = 'jst5';
1445 view.bindings = {
1446 '#test5': {
1447 attributes: [{
1448 name: 'data-name',
1449 observe: ['water', 'candy'],
1450 onGet: function(val, options) {
1451 _.each(options.observe, _.bind(function(attr, i) {
1452 equal(val[i], this.model.get(attr));
1453 }, this));
1454 equal(options.observe.toString(), 'water,candy');
1455 return model.get('water') + '-' + model.get('candy');
1456 }
1457 }]
1458 }
1459 };
1460
1461 $('#qunit-fixture').html(view.render().el);
1462
1463 equal(view.$('#test5').attr('data-name'), 'fountain-twix');
1464
1465 model.set({'water':'evian', 'candy':'snickers'});
1466 equal(view.$('#test5').attr('data-name'), 'evian-snickers');
1467 });
1468
1469 test('bindings:attributes (properties)', function() {
1470
1471 model.set({'water':true});
1472 view.model = model;
1473 view.templateId = 'jst1';
1474 view.bindings = {
1475 '#test1': {
1476 attributes: [{
1477 name: 'readonly',
1478 observe: 'water'
1479 }]
1480 }
1481 };
1482
1483 $('#qunit-fixture').html(view.render().el);
1484
1485 equal(view.$('#test1').prop('readonly'), true);
1486
1487 model.set({'water':false});
1488 equal(view.$('#test1').prop('readonly'), false);
1489 });
1490
1491 test('bindings:classes:name', function() {
1492
1493 model.set({'water':'fountain'});
1494 view.model = model;
1495 view.templateId = 'jst5';
1496 view.bindings = {
1497 '#test5': {
1498 classes: {
1499 testClass: 'water'
1500 }
1501 }
1502 };
1503
1504 $('#qunit-fixture').html(view.render().el);
1505 ok(view.$('#test5').hasClass('testClass'));
1506
1507 model.set('water', false);
1508 ok(!view.$('#test5').hasClass('testClass'));
1509 });
1510
1511 test('bindings:classes:observe', function() {
1512
1513 model.set({'truthy':false});
1514 view.model = model;
1515 view.templateId = 'jst5';
1516 view.bindings = {
1517 '#test5': {
1518 classes: {
1519 'col-md-2': {
1520 observe:'truthy'
1521 },
1522 'col-md-3': {
1523 observe: 'truthy',
1524 onGet: function(val) {
1525 return !val;
1526 }
1527 }
1528 }
1529 }
1530 };
1531
1532 $('#qunit-fixture').html(view.render().el);
1533 ok(!view.$('#test5').hasClass('col-md-2'));
1534 ok(view.$('#test5').hasClass('col-md-3'));
1535
1536 model.set('truthy', 'string is truthy');
1537 ok(view.$('#test5').hasClass('col-md-2'));
1538 ok(!view.$('#test5').hasClass('col-md-3'));
1539 });
1540
1541 test('input:number', function() {
1542
1543 model.set({'code':1});
1544 view.model = model;
1545 view.templateId = 'jst11';
1546 view.bindings = {
1547 '#test11': {
1548 observe: 'code'
1549 }
1550 };
1551 $('#qunit-fixture').html(view.render().el);
1552
1553 equal(Number(view.$('#test11').val()), 1);
1554
1555 model.set('code', 2);
1556 equal(Number(view.$('#test11').val()), 2);
1557 });
1558
1559 test('visible', 28, function() {
1560
1561 model.set({'water':false, 'candy':'twix', 'costume':false, 'visible': 'yes'});
1562 view.model = model;
1563 view.templateId = 'jst14';
1564 view.bindings = {
1565 '#test14-1': {
1566 observe: 'water',
1567 visible: true
1568 },
1569 '#test14-2': {
1570 observe: 'candy',
1571 visible: function(val, options) {
1572 equal(val, this.model.get('candy'));
1573 equal(options.observe, 'candy');
1574 return this.model.get('candy') == 'twix';
1575 }
1576 },
1577 '#test14-3': {
1578 observe: ['candy', 'costume'],
1579 visible: true,
1580 visibleFn: function($el, isVisible, options) {
1581 equal($el.attr('id'), 'test14-3');
1582 ok(isVisible);
1583 equal(options.observe.toString(), 'candy,costume');
1584 }
1585 },
1586 '#test14-4': {
1587 observe: 'visible',
1588 visible: function(val) { return val == 'yes'; },
1589 updateView: true
1590 },
1591 '#test14-5': {
1592 observe: 'visible',
1593 visible: function(val) { return val; }
1594 }
1595 };
1596 $('#qunit-fixture').html(view.render().el);
1597
1598 equal(view.$('#test14-1').css('display') == 'block' , false);
1599 equal(view.$('#test14-2').css('display') == 'block' , true);
1600 equal(view.$('#test14-2').text(), 'Test');
1601 equal(view.$('#test14-3').css('display') == 'block' , true);
1602 equal(view.$('#test14-4').css('display') == 'block' , true);
1603 equal(view.$('#test14-4').text(), 'yes');
1604 equal(view.$('#test14-5').css('display') == 'block' , true);
1605
1606 // Force-hide #test14-5 to make sure the fact that it's still visible
1607 // later is due to the truthiness check.
1608 view.$('#test14-5').hide()
1609
1610 model.set('water', true);
1611 model.set('candy', 'snickers');
1612 model.set('costume', true);
1613 model.set('visible', 'no')
1614
1615 equal(view.$('#test14-1').css('display') == 'block' , true);
1616 equal(view.$('#test14-2').css('display') == 'block' , false);
1617 equal(view.$('#test14-2').text(), 'Test');
1618 equal(view.$('#test14-3').css('display') == 'block' , true);
1619 equal(view.$('#test14-4').css('display') == 'block' , false);
1620 equal(view.$('#test14-4').text(), 'no');
1621
1622 // 'no' is still truthy.
1623 equal(view.$('#test14-5').css('display') == 'block' , true);
1624
1625 model.set('visible', false);
1626
1627 equal(view.$('#test14-5').css('display') == 'block' , false);
1628
1629 });
1630
1631 test('observe (multiple; array)', 12, function() {
1632
1633 model.set({'water':'fountain', 'candy':'twix'});
1634 view.model = model;
1635 view.templateId = 'jst5';
1636 view.bindings = {
1637 '#test5': {
1638 observe: ['water', 'candy'],
1639 onGet: function(val, options) {
1640 _.each(options.observe, _.bind(function(attr, i) {
1641 equal(val[i], this.model.get(attr));
1642 }, this));
1643 equal(options.observe.toString(), 'water,candy');
1644 return model.get('water') + ' ' + model.get('candy');
1645 }
1646 }
1647 };
1648 $('#qunit-fixture').html(view.render().el);
1649
1650 equal(view.$('#test5').text(), 'fountain twix');
1651
1652 model.set('water', 'evian');
1653 equal(view.$('#test5').text(), 'evian twix');
1654
1655 model.set('candy', 'snickers');
1656 equal(view.$('#test5').text(), 'evian snickers');
1657 });
1658
1659 test('observe (function)', 5, function() {
1660 model.set({'water':'fountain'});
1661
1662 var testView = new (Backbone.View.extend({
1663 initialize: function(options) {
1664 this.options = options;
1665 },
1666 bindings: {
1667 '#test1': {
1668 observe: function() {
1669 equal(this, testView);
1670 equal(this.options.observeValue, 'water');
1671 return this.options.observeValue;
1672 }
1673 }
1674 },
1675 render: function() {
1676 var html = document.getElementById('jst1').innerHTML;
1677 this.$el.html(_.template(html)());
1678 this.stickit();
1679 return this;
1680 }
1681 }))({
1682 model: model,
1683 observeValue: 'water'
1684 });
1685
1686 $('#qunit-fixture').html(testView.render().el);
1687
1688 equal(testView.$('#test1').val(), 'fountain');
1689
1690 model.set('water', 'evian');
1691 equal(testView.$('#test1').val(), 'evian');
1692
1693 testView.$('#test1').val('dasina').trigger('change');
1694 equal(model.get('water'), 'dasina');
1695
1696 testView.remove();
1697 });
1698
1699 test('bindings:updateView', 9, function() {
1700
1701 model.set({'water':'fountain'});
1702 view.model = model;
1703 view.templateId = 'jst1';
1704 view.bindings = {
1705 '#test1': {
1706 observe: 'water',
1707 updateView: function(val, options) {
1708 equal(options.observe, 'water');
1709 equal(val, model.get('water'));
1710 return val == 'evian';
1711 }
1712 }
1713 };
1714 $('#qunit-fixture').html(view.render().el);
1715
1716 equal(view.$('#test1').val(), '');
1717
1718 model.set({water:'evian'});
1719 equal(view.$('#test1').val(), 'evian');
1720
1721 model.set({water:'dasina'});
1722 equal(view.$('#test1').val(), 'evian');
1723 });
1724
1725 test('bindings:updateModel', 10, function() {
1726
1727 model.set({'water':'fountain'});
1728 view.model = model;
1729 view.templateId = 'jst1';
1730 view.bindings = {
1731 '#test1': {
1732 observe: 'water',
1733 updateModel: function(val, event, config) {
1734 equal(this.cid, view.cid);
1735 equal(val, view.$('#test1').val());
1736 equal(config.observe, 'water');
1737 equal(event.type, 'change');
1738 return val == 'evian';
1739 }
1740 }
1741 };
1742 $('#qunit-fixture').html(view.render().el);
1743
1744 view.$('#test1').val('dasina').trigger('change');
1745 equal(model.get('water'), 'fountain');
1746
1747 view.$('#test1').val('evian').trigger('change');
1748 equal(model.get('water'), 'evian');
1749 });
1750
1751 test('bindings:events', function() {
1752
1753 model.set({'water':'fountain'});
1754 view.model = model;
1755 view.templateId = 'jst1';
1756 view.bindings = {
1757 '#test1': {
1758 observe: 'water',
1759 events: ['blur', 'keydown']
1760 }
1761 };
1762 $('#qunit-fixture').html(view.render().el);
1763
1764 equal(view.$('#test1').val(), 'fountain');
1765
1766 // change should be overriden, so no change...
1767 view.$('#test1').val('dasina').trigger('change');
1768 equal(model.get('water'), 'fountain');
1769
1770 view.$('#test1').trigger('blur');
1771 equal(model.get('water'), 'dasina');
1772
1773 view.$('#test1').val('evian').trigger('keydown');
1774 equal(model.get('water'), 'evian');
1775 });
1776
1777 test('checkbox multiple', function() {
1778
1779 model.set({'water':['fountain', 'dasina']});
1780 view.model = model;
1781 view.templateId = 'jst18';
1782 view.bindings = {
1783 '.boxes': 'water'
1784 };
1785 $('#qunit-fixture').html(view.render().el);
1786
1787 equal(view.$('.boxes[value="fountain"]').prop('checked'), true);
1788 equal(view.$('.boxes[value="evian"]').prop('checked'), false);
1789 equal(view.$('.boxes[value="dasina"]').prop('checked'), true);
1790
1791 model.set('water', ['evian']);
1792 equal(view.$('.boxes[value="fountain"]').prop('checked'), false);
1793 equal(view.$('.boxes[value="evian"]').prop('checked'), true);
1794 equal(view.$('.boxes[value="dasina"]').prop('checked'), false);
1795
1796 view.$('.boxes[value="dasina"]').prop('checked', true).trigger('change');
1797 equal(model.get('water').length, 2);
1798 equal(_.indexOf(model.get('water'), 'evian') > -1, true);
1799 equal(_.indexOf(model.get('water'), 'dasina') > -1, true);
1800 });
1801
1802 test('checkbox (single with value defined)', function() {
1803
1804 model.set({'water':null});
1805 view.model = model;
1806 view.templateId = 'jst19';
1807 view.bindings = {
1808 '.box': 'water'
1809 };
1810 $('#qunit-fixture').html(view.render().el);
1811
1812 equal(view.$('.box').prop('checked'), false);
1813
1814 model.set('water', 'fountain');
1815 equal(view.$('.box').prop('checked'), true);
1816
1817 view.$('.box').prop('checked', false).trigger('change');
1818 equal(model.get('water'), null);
1819 });
1820
1821 test('getVal', 5, function() {
1822
1823 model.set({'water':'fountain'});
1824 view.model = model;
1825 view.templateId = 'jst1';
1826 view.bindings = {
1827 '#test1': {
1828 observe: 'water',
1829 getVal: function($el, event, options) {
1830 equal(this.cid, view.cid);
1831 equal($el.attr('id'), 'test1');
1832 equal(event.type, 'change');
1833 equal(options.observe, 'water');
1834 return 'test-' + $el.val();
1835 }
1836 }
1837 };
1838 $('#qunit-fixture').html(view.render().el);
1839
1840 view.$('#test1').val('dasina').trigger('change');
1841 equal(model.get('water'), 'test-dasina');
1842 });
1843
1844 test('update', 8, function() {
1845
1846 model.set({'water':'fountain'});
1847 view.model = model;
1848 view.templateId = 'jst1';
1849 view.bindings = {
1850 '#test1': {
1851 observe: 'water',
1852 update: function($el, val, model, options) {
1853 equal($el.attr('id'), 'test1');
1854 equal(val, model.get('water'));
1855 equal(options.observe, 'water');
1856 $el.val('test-' + val);
1857 }
1858 }
1859 };
1860 $('#qunit-fixture').html(view.render().el);
1861
1862 equal(view.$('#test1').val(), 'test-fountain');
1863
1864 model.set('water', 'dasina');
1865 equal(view.$('#test1').val(), 'test-dasina');
1866 });
1867
1868 test('initialize', 3, function() {
1869
1870 model.set({'water':'fountain'});
1871 view.model = model;
1872 view.templateId = 'jst1';
1873 view.bindings = {
1874 '#test1': {
1875 observe: 'water',
1876 initialize: function($el, model, options) {
1877 equal($el.val(), 'fountain');
1878 equal(model.get('water'), 'fountain');
1879 equal(options.observe, 'water');
1880 }
1881 }
1882 };
1883 $('#qunit-fixture').html(view.render().el);
1884 });
1885
1886 test('destroy', 3, function() {
1887
1888 model.set({'water':'fountain'});
1889 view.model = model;
1890 view.templateId = 'jst1';
1891 view.bindings = {
1892 '#test1': {
1893 observe: 'water',
1894 events: ['change', 'input', 'keyup'],
1895 destroy: function($el, model, options) {
1896 equal($el.val(), 'fountain');
1897 equal(model.get('water'), 'fountain');
1898 equal(options.observe, 'water');
1899 }
1900 }
1901 };
1902 $('#qunit-fixture').html(view.render().el);
1903 view.unstickit();
1904 });
1905
1906 test('null form value', function() {
1907
1908 model.set({'water':null});
1909 view.model = model;
1910 view.templateId = 'jst1';
1911 view.bindings = {
1912 '#test1': 'water'
1913 };
1914 $('#qunit-fixture').html(view.render().el);
1915 equal(view.$('#test1').val(), '');
1916 });
1917
1918 test('null html value', function() {
1919
1920 model.set({'water':null});
1921 view.model = model;
1922 view.templateId = 'jst5';
1923 view.bindings = {
1924 '#test5': 'water'
1925 };
1926 $('#qunit-fixture').html(view.render().el);
1927 equal(view.$('#test5').html(), '');
1928 });
1929
1930 test('stickitChange', 1, function() {
1931
1932 model.set({'water':null});
1933 view.model = model;
1934 view.templateId = 'jst1';
1935 view.bindings = {
1936 '#test1': 'water'
1937 };
1938 $('#qunit-fixture').html(view.render().el);
1939
1940 model.on('change:water', function(m, v, options) {
1941 ok(options.stickitChange);
1942 });
1943
1944 view.$('#test1').val('dasina').trigger('change');
1945 });
1946
1947 test('view.remove should be called and unbind events', 4, function() {
1948
1949 model.set({'water':null});
1950 view.model = model;
1951 view.templateId = 'jst1';
1952 view.bindings = {
1953 '#test1': 'water'
1954 };
1955 view.remove = function(option) {
1956 ok(true);
1957 ok(!!option);
1958 };
1959 $('#qunit-fixture').html(view.render().el);
1960 equal(view._modelBindings.length, 1);
1961 view.remove('test');
1962 equal(view._modelBindings.length, 0);
1963 });
1964
1965 });