forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindingPreprocessingBehaviors.js
74 lines (67 loc) · 2.8 KB
/
bindingPreprocessingBehaviors.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
describe('Binding preprocessing', function() {
it('Should allow binding to modify value through "preprocess" method', function() {
delete ko.bindingHandlers.a;
// create binding that has a default value of false
ko.bindingHandlers.b = {
preprocess: function(value) {
return value || "false";
}
};
var rewritten = ko.expressionRewriting.preProcessBindings("a: 1, b");
var parsedRewritten = eval("({" + rewritten + "})");
expect(parsedRewritten.a).toEqual(1);
expect(parsedRewritten.b).toEqual(false);
});
it('Should allow binding to add/replace bindings through "preprocess" method\'s "addBinding" callback', function() {
ko.bindingHandlers.a = {
preprocess: function(value, key, addBinding) {
// the a binding will be copied to a2
addBinding(key+"2", value);
return value;
}
};
ko.bindingHandlers.b = {
preprocess: function(value, key, addBinding) {
// the b binding will be replaced by b2
addBinding(key+"2", value);
}
};
var rewritten = ko.expressionRewriting.preProcessBindings("a: 1, b: 2");
var parsedRewritten = eval("({" + rewritten + "})");
expect(parsedRewritten.a).toEqual(1);
expect(parsedRewritten.a2).toEqual(1);
expect(parsedRewritten.b).toBeUndefined();
expect(parsedRewritten.b2).toEqual(2);
});
it('Should be able to chain "preprocess" calls when one adds a binding for another', function() {
ko.bindingHandlers.a = {
preprocess: function(value, key, addBinding) {
// replace with b
addBinding("b", value);
}
};
ko.bindingHandlers.b = {
preprocess: function(value, key, addBinding) {
// adds 1 to value
return '' + (+value + 1);
}
};
var rewritten = ko.expressionRewriting.preProcessBindings("a: 2");
var parsedRewritten = eval("({" + rewritten + "})");
expect(parsedRewritten.a).toBeUndefined();
expect(parsedRewritten.b).toEqual(3);
});
it('Should be able to get a dynamically created binding handler during preprocessing', function() {
this.restoreAfter(ko, 'getBindingHandler'); // restore original function when done
ko.getBindingHandler = function(bindingKey) {
return {
preprocess: function(value) {
return value + '2';
}
};
};
var rewritten = ko.expressionRewriting.preProcessBindings("a: 1");
var parsedRewritten = eval("({" + rewritten + "})");
expect(parsedRewritten.a).toEqual(12);
});
});