/
data_binding_adapter.js
60 lines (49 loc) · 1.36 KB
/
data_binding_adapter.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
rivets.adapters['^'] = {
subscribe: function(obj, key, callback) {
obj.on('change', callback);
},
unsubscribe: function(obj, key, callback) {
obj.off('change', callback);
},
read: function(obj, key) {
return obj['get' + capitalize(key)]();
},
publish: function(obj, key, value) {
obj['set' + capitalize(key)](value);
}
};
rivets.binders.toggle = {
bind: function(el) {
var adapter = this.getAdapter();
this.callback = _.bind(function() {
var value = adapter.read(this.model, this.key.path);
adapter.publish(this.model, this.key.path, !value);
}, this);
$(el).on('click', this.callback);
},
unbind: function(el) {
$(el).off('click', this.callback);
},
routine: function(el, value) {
$(el)[value ? 'addClass' : 'removeClass']('btn-inverse');
}
};
rivets.binders.enable = function(el, value) {
$(el).find('.btn-object-action')
.prop('disabled', value ? false : true);
};
rivets.binders.val = {
bind: function(el) {
this.callback = _.bind(function(e) {
this.getAdapter().publish(this.model, this.key.path, e.target.value);
}, this);
$(el).on('keyup', this.callback);
},
unbind: function(el) {
$(el).off('keyup', this.callback);
},
routine: rivets.binders.value.routine
};
rivets.formatters.toggle = function(value, str1, str2) {
return (value ? str1 : str2);
};