Repository navigation
Expand file tree
/
Copy pathbindings.js
More file actions
155 lines (142 loc) · 4.4 KB
/
Copy pathbindings.js
File metadata and controls
155 lines (142 loc) · 4.4 KB
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
// Generated by CoffeeScript 1.11.1
(function() {
var BindingsClass, BindingsInstance, ValueSetter;
BindingsClass = {
model: 'model',
bindings: {}
};
ValueSetter = (function() {
function ValueSetter(context) {
this.context = context;
}
ValueSetter.prototype.setValue = function(element, value, setter) {
if (typeof setter === 'string') {
setter = this.context.proxy(this.context[setter]);
}
setter = setter || (function(_this) {
return function(e, v) {
return _this._standardSetter(e, v);
};
})(this);
return setter(element, value);
};
ValueSetter.prototype.getValue = function(element, getter) {
if (typeof getter === 'string') {
getter = this.context.proxy(this.context[getter]);
}
getter = getter || (function(_this) {
return function(e, v) {
return _this._standardGetter(e, v);
};
})(this);
return getter(element);
};
ValueSetter.prototype._standardGetter = function(element) {
var name, self;
self = this;
return (typeof self[name = "_" + (element.attr('type')) + "Get"] === "function" ? self[name](element) : void 0) || element.val();
};
ValueSetter.prototype._standardSetter = function(element, value) {
var self;
self = this;
return element.each(function() {
var el, name;
el = $(this);
return (typeof self[name = "_" + (el.attr('type')) + "Set"] === "function" ? self[name](el, value) : void 0) || el.val(value);
});
};
ValueSetter.prototype._checkboxSet = function(element, value) {
if (value) {
return element.prop('checked', 'checked');
} else {
return element.prop('checked', '');
}
};
ValueSetter.prototype._checkboxGet = function(element) {
return element.is(':checked');
};
return ValueSetter;
})();
BindingsInstance = {
getModel: function() {
return this[this.modelVar];
},
setModel: function(model) {
return this[this.modelVar] = model;
},
walkBindings: function(fn) {
var field, ref, results, selector;
ref = this.bindings;
results = [];
for (selector in ref) {
field = ref[selector];
results.push(fn(selector, field));
}
return results;
},
applyBindings: function() {
this.valueSetter = new ValueSetter(this);
return this.walkBindings((function(_this) {
return function(selector, field) {
if (!field.direction || field.direction === 'model') {
_this._bindModelToEl(_this.getModel(), field, selector);
}
if (!field.direction || field.direction === 'element') {
return _this._bindElToModel(_this.getModel(), field, selector);
}
};
})(this));
},
_getField: function(value) {
if (typeof value === 'string') {
return value;
} else {
return value.field;
}
},
_forceModelBindings: function(model) {
return this.walkBindings((function(_this) {
return function(selector, field) {
return _this.valueSetter.setValue(_this.$(selector), model[_this._getField(field)], field.setter);
};
})(this));
},
changeBindingSource: function(model) {
this.getModel().off('change');
this.walkBindings((function(_this) {
return function(selector) {
if (selector === 'self') {
selector = false;
}
return _this.el.off('change', selector);
};
})(this));
this.setModel(model);
this._forceModelBindings(model);
return this.applyBindings();
},
_bindModelToEl: function(model, field, selector) {
var self;
self = this;
if (selector === 'self') {
selector = false;
}
return this.el.on('change', selector, function() {
return model[self._getField(field)] = self.valueSetter.getValue($(this), field.getter);
});
},
_bindElToModel: function(model, field, selector) {
return model.on('change', (function(_this) {
return function() {
return _this.valueSetter.setValue(_this.$(selector), model[_this._getField(field)], field.setter);
};
})(this));
}
};
Spine.Bindings = {
extended: function() {
this.extend(BindingsClass);
return this.include(BindingsInstance);
}
};
}).call(this);