/
binding.js
474 lines (373 loc) · 13.3 KB
/
binding.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
// ========================================================================
// SC.Binding Tests
// ========================================================================
/*globals module test ok isObj equals expects */
var fromObject, toObject, binding, Bon1, bon2 ; // global variables
module("basic object binding", {
setup: function() {
fromObject = SC.Object.create({ value: 'start' }) ;
toObject = SC.Object.create({ value: 'end' }) ;
binding = SC.Binding.from("value", fromObject).to("value", toObject).connect() ;
SC.Binding.flushPendingChanges() ; // actually sets up up the connection
}
});
test("binding is connected", function() {
equals(binding.isConnected, YES, "binding.isConnected") ;
});
test("binding has actually been setup", function() {
equals(binding._connectionPending, NO, "binding._connectionPending") ;
});
test("binding should have synced on connect", function() {
equals(toObject.get("value"), "start", "toObject.value should match fromObject.value");
});
test("changing fromObject should mark binding as dirty", function() {
fromObject.set("value", "change") ;
equals(binding._changePending, YES) ;
});
test("fromObject change should propogate to toObject only after flush", function() {
fromObject.set("value", "change") ;
equals(toObject.get("value"), "start") ;
SC.Binding.flushPendingChanges() ;
equals(toObject.get("value"), "change") ;
});
test("changing toObject should mark binding as dirty", function() {
toObject.set("value", "change") ;
equals(binding._changePending, YES) ;
});
test("toObject change should propogate to fromObject only after flush", function() {
toObject.set("value", "change") ;
equals(fromObject.get("value"), "start") ;
SC.Binding.flushPendingChanges() ;
equals(fromObject.get("value"), "change") ;
});
test("suspended observing during bindings", function() {
// setup special binding
fromObject = SC.Object.create({
value1: 'value1',
value2: 'value2'
});
toObject = SC.Object.create({
value1: 'value1',
value2: 'value2',
callCount: 0,
observer: function() {
equals(this.get('value1'), 'CHANGED', 'value1 when observer fires');
equals(this.get('value2'), 'CHANGED', 'value2 when observer fires');
this.callCount++;
}.observes('value1', 'value2')
});
toObject.bind('value1', fromObject, 'value1');
toObject.bind('value2', fromObject, 'value2');
// change both value1 + value2, then flush bindings. observer should only
// fire after bindings are done flushing.
fromObject.set('value1', 'CHANGED').set('value2', 'CHANGED');
SC.Binding.flushPendingChanges();
equals(toObject.callCount, 2, 'should call observer twice');
});
test("binding will disconnect", function() {
binding.disconnect();
equals(binding.isConnected, NO, "binding.isConnected");
});
test("binding disconnection actually works", function() {
binding.disconnect();
fromObject.set('value', 'change');
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), 'start');
});
module("bindings on classes");
test("should connect when multiple instances of class are created", function() {
window.TestNamespace = {};
window.TestNamespace.stubController = SC.Object.create({
name: 'How to Be Happy'
});
try {
var myClass = SC.Object.extend({
fooBinding: SC.Binding.from('TestNamespace.stubController.name')
});
var myFirstObj;
SC.run(function() { myFirstObj = myClass.create(); });
equals(myFirstObj.get('foo'), "How to Be Happy");
var mySecondObj;
SC.run(function() { mySecondObj = myClass.create() });
equals(mySecondObj.get('foo'), "How to Be Happy");
} finally {
window.TestNamespace = undefined;
}
});
module("one way binding", {
setup: function() {
fromObject = SC.Object.create({ value: 'start' }) ;
toObject = SC.Object.create({ value: 'end' }) ;
binding = SC.Binding.from("value", fromObject).to("value", toObject).oneWay().connect() ;
SC.Binding.flushPendingChanges() ; // actually sets up up the connection
}
});
test("changing fromObject should mark binding as dirty", function() {
fromObject.set("value", "change") ;
equals(binding._changePending, YES) ;
});
test("fromObject change should propogate after flush", function() {
fromObject.set("value", "change") ;
equals(toObject.get("value"), "start") ;
SC.Binding.flushPendingChanges() ;
equals(toObject.get("value"), "change") ;
});
test("changing toObject should not make binding dirty", function() {
toObject.set("value", "change") ;
equals(binding._changePending, NO) ;
});
test("toObject change should NOT propogate", function() {
toObject.set("value", "change") ;
equals(fromObject.get("value"), "start") ;
SC.Binding.flushPendingChanges() ;
equals(fromObject.get("value"), "start") ;
});
var first, second, third, binding1, binding2 ; // global variables
module("chained binding", {
setup: function() {
first = SC.Object.create({ output: 'first' }) ;
second = SC.Object.create({
input: 'second',
output: 'second',
inputDidChange: function() {
this.set("output", this.get("input")) ;
}.observes("input")
}) ;
third = SC.Object.create({ input: "third" }) ;
binding1 = SC.Binding.from("output", first).to("input", second).connect() ;
binding2 = SC.Binding.from("output", second).to("input", third).connect() ;
SC.Binding.flushPendingChanges() ; // actually sets up up the connection
}
});
test("changing first output should propograte to third after flush", function() {
first.set("output", "change") ;
equals("change", first.get("output"), "first.output") ;
ok("change" !== third.get("input"), "third.input") ;
var didChange = YES;
while(didChange) didChange = SC.Binding.flushPendingChanges() ;
// bindings should not have bending changes
equals(binding1._changePending, NO, "binding1._changePending") ;
equals(binding2._changePending, NO, "binding2._changePending") ;
equals("change", first.get("output"), "first.output") ;
equals("change", second.get("input"), "second.input") ;
equals("change", second.get("output"), "second.output") ;
equals("change", third.get("input"), "third.input") ;
});
module("Custom Binding", {
setup: function() {
Bon1 = SC.Object.extend({
value1: "hi",
value2: 83,
array1: []
});
bon2 = SC.Object.create({
val1: "hello",
val2: 25,
arr: [1,2,3,4]
});
TestNamespace = {
bon2: bon2,
Bon1: Bon1
} ;
},
teardown: function() {
bon2.destroy();
}
});
test("Binding value1 such that it will recieve only single values", function() {
var bon1 = Bon1.create({
value1Binding: SC.Binding.single("TestNamespace.bon2.val1"),
array1Binding: SC.Binding.single("TestNamespace.bon2.arr")
});
SC.Binding.flushPendingChanges();
var a = [23,31,12,21];
bon2.set("arr", a);
bon2.set("val1","changed");
SC.Binding.flushPendingChanges();
equals(bon2.get("val1"),bon1.get("value1"));
equals("@@MULT@@",bon1.get("array1"));
bon1.destroy();
});
test("Single binding using notEmpty function.", function() {
var bond = Bon1.create ({
array1Binding: SC.Binding.single("TestNamespace.bon2.arr").notEmpty(null,'(EMPTY)')
});
SC.Binding.flushPendingChanges();
bon2.set("arr", []);
SC.Binding.flushPendingChanges();
equals("(EMPTY)",bond.get("array1"));
});
test("Binding with transforms, function to check the type of value", function() {
var jon = Bon1.create({
value1Binding: SC.Binding.transform(function(val1) {
return (SC.typeOf(val1) == SC.T_STRING)? val1 : "";
}).from("TestNamespace.bon2.val1")
});
SC.Binding.flushPendingChanges();
bon2.set("val1","changed");
SC.Binding.flushPendingChanges();
equals(jon.get("value1"), bon2.get("val1"));
});
test("two bindings to the same value should sync in the order they are initialized", function() {
SC.LOG_BINDINGS = YES;
SC.RunLoop.begin();
window.a = SC.Object.create({
foo: "bar"
});
var a = window.a;
window.b = SC.Object.create({
foo: "baz",
fooBinding: "a.foo",
C: SC.Object.extend({
foo: "bee",
fooBinding: "*owner.foo"
}),
init: function() {
sc_super();
this.set('c', this.C.create({ owner: this }));
}
});
var b = window.b;
SC.LOG_BINDINGS = NO;
SC.RunLoop.end();
equals(a.get('foo'), "bar", 'a.foo should not change');
equals(b.get('foo'), "bar", 'a.foo should propogate up to b.foo');
equals(b.c.get('foo'), "bar", 'a.foo should propogate up to b.c.foo');
window.a = window.b = null ;
});
module("AND binding", {
setup: function() {
// temporarily set up two source objects in the SC namespace so we can
// use property paths to access them
SC.testControllerA = SC.Object.create({ value: NO });
SC.testControllerB = SC.Object.create({ value: NO });
toObject = SC.Object.create({
value: null,
valueBinding: SC.Binding.and('SC.testControllerA.value', 'SC.testControllerB.value')
});
},
teardown: function() {
SC.testControllerA.destroy();
SC.testControllerB.destroy();
}
});
test("toObject.value should be YES if both sources are YES", function() {
SC.RunLoop.begin();
SC.testControllerA.set('value', YES);
SC.testControllerB.set('value', YES);
SC.RunLoop.end();
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), YES);
});
test("toObject.value should be NO if either source is NO", function() {
SC.RunLoop.begin();
SC.testControllerA.set('value', YES);
SC.testControllerB.set('value', NO);
SC.RunLoop.end();
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), NO);
SC.RunLoop.begin();
SC.testControllerA.set('value', YES);
SC.testControllerB.set('value', YES);
SC.RunLoop.end();
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), YES);
SC.RunLoop.begin();
SC.testControllerA.set('value', NO);
SC.testControllerB.set('value', YES);
SC.RunLoop.end();
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), NO);
});
module("OR binding", {
setup: function() {
// temporarily set up two source objects in the SC namespace so we can
// use property paths to access them
SC.testControllerA = SC.Object.create({ value: NO });
SC.testControllerB = SC.Object.create({ value: null });
toObject = SC.Object.create({
value: null,
valueBinding: SC.Binding.or('SC.testControllerA.value', 'SC.testControllerB.value')
});
},
teardown: function() {
SC.testControllerA.destroy();
SC.testControllerB.destroy();
}
});
test("toObject.value should be first value if first value is truthy", function() {
SC.RunLoop.begin();
SC.testControllerA.set('value', 'first value');
SC.testControllerB.set('value', 'second value');
SC.RunLoop.end();
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), 'first value');
});
test("toObject.value should be second value if first is falsy", function() {
SC.RunLoop.begin();
SC.testControllerA.set('value', NO);
SC.testControllerB.set('value', 'second value');
SC.RunLoop.end();
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), 'second value');
});
module("Binding with '[]'", {
setup: function() {
fromObject = SC.Object.create({ value: [] });
toObject = SC.Object.create({ value: '' });
binding = SC.Binding.transform(function(v) {
return v ? v.join(',') : '';
}).from("value.[]", fromObject).to("value", toObject).connect();
}
});
test("Binding refreshes after a couple of items have been pushed in the array", function() {
fromObject.get('value').pushObjects(['foo', 'bar']);
SC.Binding.flushPendingChanges();
equals(toObject.get('value'), 'foo,bar');
});
module("propertyNameBinding with longhand", {
setup: function(){
TestNamespace = {
fromObject: SC.Object.create({
value: "originalValue"
}),
toObject: SC.Object.create({
valueBinding: SC.Binding.from('TestNamespace.fromObject.value'),
localValue: "originalLocal",
relativeBinding: SC.Binding.from('.localValue')
})
};
},
teardown: function(){
TestNamespace.fromObject.destroy();
TestNamespace.toObject.destroy();
delete TestNamespace.fromObject;
delete TestNamespace.toObject;
}
});
test("works with full path", function(){
SC.RunLoop.begin();
TestNamespace.fromObject.set('value', "updatedValue");
SC.RunLoop.end();
equals(TestNamespace.toObject.get('value'), "updatedValue");
SC.RunLoop.begin();
TestNamespace.fromObject.set('value', "newerValue");
SC.RunLoop.end();
equals(TestNamespace.toObject.get('value'), "newerValue");
});
test("works with local path", function(){
SC.RunLoop.begin();
TestNamespace.toObject.set('localValue', "updatedValue");
SC.RunLoop.end();
equals(TestNamespace.toObject.get('relative'), "updatedValue");
SC.RunLoop.begin();
TestNamespace.toObject.set('localValue', "newerValue");
SC.RunLoop.end();
equals(TestNamespace.toObject.get('relative'), "newerValue");
});