Build 0.5.0.
Showing
6 changed files
with
135 additions
and
59 deletions
... | @@ -2,9 +2,9 @@ | ... | @@ -2,9 +2,9 @@ |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "repo": "mikeric/rivets", | 3 | "repo": "mikeric/rivets", |
4 | "description": "Declarative data binding facility.", | 4 | "description": "Declarative data binding facility.", |
5 | "version": "0.4.9", | 5 | "version": "0.5.0", |
6 | "keywords": ["data binding", "templating"], | 6 | "keywords": ["data binding", "templating"], |
7 | "scripts": ["lib/rivets.js"], | 7 | "scripts": ["dist/rivets.js"], |
8 | "main": "lib/rivets.js", | 8 | "main": "dist/rivets.js", |
9 | "license": "MIT" | 9 | "license": "MIT" |
10 | } | 10 | } | ... | ... |
1 | // rivets.js | 1 | // rivets.js |
2 | // version: 0.4.9 | 2 | // version: 0.5.0 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
... | @@ -17,28 +17,24 @@ | ... | @@ -17,28 +17,24 @@ |
17 | } | 17 | } |
18 | 18 | ||
19 | Rivets.Binding = (function() { | 19 | Rivets.Binding = (function() { |
20 | 20 | function Binding(view, el, type, key, keypath, options) { | |
21 | function Binding(el, type, model, keypath, options) { | ||
22 | var identifier, regexp, value, _ref; | 21 | var identifier, regexp, value, _ref; |
22 | |||
23 | this.view = view; | ||
23 | this.el = el; | 24 | this.el = el; |
24 | this.type = type; | 25 | this.type = type; |
25 | this.model = model; | 26 | this.key = key; |
26 | this.keypath = keypath; | 27 | this.keypath = keypath; |
27 | this.options = options != null ? options : {}; | 28 | this.options = options != null ? options : {}; |
29 | this.update = __bind(this.update, this); | ||
28 | this.unbind = __bind(this.unbind, this); | 30 | this.unbind = __bind(this.unbind, this); |
29 | |||
30 | this.bind = __bind(this.bind, this); | 31 | this.bind = __bind(this.bind, this); |
31 | |||
32 | this.publish = __bind(this.publish, this); | 32 | this.publish = __bind(this.publish, this); |
33 | |||
34 | this.sync = __bind(this.sync, this); | 33 | this.sync = __bind(this.sync, this); |
35 | |||
36 | this.set = __bind(this.set, this); | 34 | this.set = __bind(this.set, this); |
37 | |||
38 | this.formattedValue = __bind(this.formattedValue, this); | 35 | this.formattedValue = __bind(this.formattedValue, this); |
39 | 36 | if (!(this.binder = this.view.binders[type])) { | |
40 | if (!(this.binder = Rivets.binders[type])) { | 37 | _ref = this.view.binders; |
41 | _ref = Rivets.binders; | ||
42 | for (identifier in _ref) { | 38 | for (identifier in _ref) { |
43 | value = _ref[identifier]; | 39 | value = _ref[identifier]; |
44 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | 40 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { |
... | @@ -51,23 +47,25 @@ | ... | @@ -51,23 +47,25 @@ |
51 | } | 47 | } |
52 | } | 48 | } |
53 | } | 49 | } |
54 | this.binder || (this.binder = Rivets.binders['*']); | 50 | this.binder || (this.binder = this.view.binders['*']); |
55 | if (this.binder instanceof Function) { | 51 | if (this.binder instanceof Function) { |
56 | this.binder = { | 52 | this.binder = { |
57 | routine: this.binder | 53 | routine: this.binder |
58 | }; | 54 | }; |
59 | } | 55 | } |
60 | this.formatters = this.options.formatters || []; | 56 | this.formatters = this.options.formatters || []; |
57 | this.model = this.view.models[this.key]; | ||
61 | } | 58 | } |
62 | 59 | ||
63 | Binding.prototype.formattedValue = function(value) { | 60 | Binding.prototype.formattedValue = function(value) { |
64 | var args, formatter, id, _i, _len, _ref, _ref1, _ref2, _ref3; | 61 | var args, formatter, id, _i, _len, _ref, _ref1, _ref2, _ref3; |
62 | |||
65 | _ref = this.formatters; | 63 | _ref = this.formatters; |
66 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 64 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
67 | formatter = _ref[_i]; | 65 | formatter = _ref[_i]; |
68 | args = formatter.split(/\s+/); | 66 | args = formatter.split(/\s+/); |
69 | id = args.shift(); | 67 | id = args.shift(); |
70 | formatter = this.model[id] instanceof Function ? this.model[id] : ((_ref1 = this.options) != null ? (_ref2 = _ref1.bindingOptions) != null ? (_ref3 = _ref2.formatters) != null ? _ref3[id] : void 0 : void 0 : void 0) instanceof Function ? this.options.bindingOptions.formatters[id] : Rivets.formatters[id]; | 68 | formatter = this.model[id] instanceof Function ? this.model[id] : ((_ref1 = this.options) != null ? (_ref2 = _ref1.bindingOptions) != null ? (_ref3 = _ref2.formatters) != null ? _ref3[id] : void 0 : void 0 : void 0) instanceof Function ? this.options.bindingOptions.formatters[id] : this.view.formatters[id]; |
71 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { | 69 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { |
72 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); | 70 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); |
73 | } else if (formatter instanceof Function) { | 71 | } else if (formatter instanceof Function) { |
... | @@ -79,39 +77,42 @@ | ... | @@ -79,39 +77,42 @@ |
79 | 77 | ||
80 | Binding.prototype.set = function(value) { | 78 | Binding.prototype.set = function(value) { |
81 | var _ref; | 79 | var _ref; |
80 | |||
82 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); | 81 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); |
83 | return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0; | 82 | return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0; |
84 | }; | 83 | }; |
85 | 84 | ||
86 | Binding.prototype.sync = function() { | 85 | Binding.prototype.sync = function() { |
87 | return this.set(this.options.bypass ? this.model[this.keypath] : Rivets.config.adapter.read(this.model, this.keypath)); | 86 | return this.set(this.options.bypass ? this.model[this.keypath] : this.view.config.adapter.read(this.model, this.keypath)); |
88 | }; | 87 | }; |
89 | 88 | ||
90 | Binding.prototype.publish = function() { | 89 | Binding.prototype.publish = function() { |
91 | var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2; | 90 | var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2; |
91 | |||
92 | value = getInputValue(this.el); | 92 | value = getInputValue(this.el); |
93 | _ref = this.formatters.slice(0).reverse(); | 93 | _ref = this.formatters.slice(0).reverse(); |
94 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 94 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
95 | formatter = _ref[_i]; | 95 | formatter = _ref[_i]; |
96 | args = formatter.split(/\s+/); | 96 | args = formatter.split(/\s+/); |
97 | id = args.shift(); | 97 | id = args.shift(); |
98 | if ((_ref1 = Rivets.formatters[id]) != null ? _ref1.publish : void 0) { | 98 | if ((_ref1 = this.view.formatters[id]) != null ? _ref1.publish : void 0) { |
99 | value = (_ref2 = Rivets.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); | 99 | value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); |
100 | } | 100 | } |
101 | } | 101 | } |
102 | return Rivets.config.adapter.publish(this.model, this.keypath, value); | 102 | return this.view.config.adapter.publish(this.model, this.keypath, value); |
103 | }; | 103 | }; |
104 | 104 | ||
105 | Binding.prototype.bind = function() { | 105 | Binding.prototype.bind = function() { |
106 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | 106 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; |
107 | |||
107 | if ((_ref = this.binder.bind) != null) { | 108 | if ((_ref = this.binder.bind) != null) { |
108 | _ref.call(this, this.el); | 109 | _ref.call(this, this.el); |
109 | } | 110 | } |
110 | if (this.options.bypass) { | 111 | if (this.options.bypass) { |
111 | this.sync(); | 112 | this.sync(); |
112 | } else { | 113 | } else { |
113 | Rivets.config.adapter.subscribe(this.model, this.keypath, this.sync); | 114 | this.view.config.adapter.subscribe(this.model, this.keypath, this.sync); |
114 | if (Rivets.config.preloadData) { | 115 | if (this.view.config.preloadData) { |
115 | this.sync(); | 116 | this.sync(); |
116 | } | 117 | } |
117 | } | 118 | } |
... | @@ -128,7 +129,7 @@ | ... | @@ -128,7 +129,7 @@ |
128 | model = this.view.models[dependency.shift()]; | 129 | model = this.view.models[dependency.shift()]; |
129 | keypath = dependency.join('.'); | 130 | keypath = dependency.join('.'); |
130 | } | 131 | } |
131 | _results.push(Rivets.config.adapter.subscribe(model, keypath, this.sync)); | 132 | _results.push(this.view.config.adapter.subscribe(model, keypath, this.sync)); |
132 | } | 133 | } |
133 | return _results; | 134 | return _results; |
134 | } | 135 | } |
... | @@ -136,11 +137,12 @@ | ... | @@ -136,11 +137,12 @@ |
136 | 137 | ||
137 | Binding.prototype.unbind = function() { | 138 | Binding.prototype.unbind = function() { |
138 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | 139 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; |
140 | |||
139 | if ((_ref = this.binder.unbind) != null) { | 141 | if ((_ref = this.binder.unbind) != null) { |
140 | _ref.call(this, this.el); | 142 | _ref.call(this, this.el); |
141 | } | 143 | } |
142 | if (!this.options.bypass) { | 144 | if (!this.options.bypass) { |
143 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | 145 | this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync); |
144 | } | 146 | } |
145 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | 147 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { |
146 | _ref2 = this.options.dependencies; | 148 | _ref2 = this.options.dependencies; |
... | @@ -155,45 +157,66 @@ | ... | @@ -155,45 +157,66 @@ |
155 | model = this.view.models[dependency.shift()]; | 157 | model = this.view.models[dependency.shift()]; |
156 | keypath = dependency.join('.'); | 158 | keypath = dependency.join('.'); |
157 | } | 159 | } |
158 | _results.push(Rivets.config.adapter.unsubscribe(model, keypath, this.sync)); | 160 | _results.push(this.view.config.adapter.unsubscribe(model, keypath, this.sync)); |
159 | } | 161 | } |
160 | return _results; | 162 | return _results; |
161 | } | 163 | } |
162 | }; | 164 | }; |
163 | 165 | ||
166 | Binding.prototype.update = function() { | ||
167 | this.unbind(); | ||
168 | this.model = this.view.models[this.key]; | ||
169 | return this.bind(); | ||
170 | }; | ||
171 | |||
164 | return Binding; | 172 | return Binding; |
165 | 173 | ||
166 | })(); | 174 | })(); |
167 | 175 | ||
168 | Rivets.View = (function() { | 176 | Rivets.View = (function() { |
169 | |||
170 | function View(els, models, options) { | 177 | function View(els, models, options) { |
178 | var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3; | ||
179 | |||
171 | this.els = els; | 180 | this.els = els; |
172 | this.models = models; | 181 | this.models = models; |
173 | this.options = options; | 182 | this.options = options != null ? options : {}; |
183 | this.update = __bind(this.update, this); | ||
174 | this.publish = __bind(this.publish, this); | 184 | this.publish = __bind(this.publish, this); |
175 | |||
176 | this.sync = __bind(this.sync, this); | 185 | this.sync = __bind(this.sync, this); |
177 | |||
178 | this.unbind = __bind(this.unbind, this); | 186 | this.unbind = __bind(this.unbind, this); |
179 | |||
180 | this.bind = __bind(this.bind, this); | 187 | this.bind = __bind(this.bind, this); |
181 | |||
182 | this.select = __bind(this.select, this); | 188 | this.select = __bind(this.select, this); |
183 | |||
184 | this.build = __bind(this.build, this); | 189 | this.build = __bind(this.build, this); |
185 | |||
186 | this.bindingRegExp = __bind(this.bindingRegExp, this); | 190 | this.bindingRegExp = __bind(this.bindingRegExp, this); |
187 | |||
188 | if (!(this.els.jquery || this.els instanceof Array)) { | 191 | if (!(this.els.jquery || this.els instanceof Array)) { |
189 | this.els = [this.els]; | 192 | this.els = [this.els]; |
190 | } | 193 | } |
194 | _ref = ['config', 'binders', 'formatters']; | ||
195 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
196 | option = _ref[_i]; | ||
197 | this[option] = {}; | ||
198 | if (this.options[option]) { | ||
199 | _ref1 = this.options[option]; | ||
200 | for (k in _ref1) { | ||
201 | v = _ref1[k]; | ||
202 | this[option][k] = v; | ||
203 | } | ||
204 | } | ||
205 | _ref2 = Rivets[option]; | ||
206 | for (k in _ref2) { | ||
207 | v = _ref2[k]; | ||
208 | if ((_ref3 = (_base = this[option])[k]) == null) { | ||
209 | _base[k] = v; | ||
210 | } | ||
211 | } | ||
212 | } | ||
191 | this.build(); | 213 | this.build(); |
192 | } | 214 | } |
193 | 215 | ||
194 | View.prototype.bindingRegExp = function() { | 216 | View.prototype.bindingRegExp = function() { |
195 | var prefix; | 217 | var prefix; |
196 | prefix = Rivets.config.prefix; | 218 | |
219 | prefix = this.config.prefix; | ||
197 | if (prefix) { | 220 | if (prefix) { |
198 | return new RegExp("^data-" + prefix + "-"); | 221 | return new RegExp("^data-" + prefix + "-"); |
199 | } else { | 222 | } else { |
... | @@ -204,19 +227,21 @@ | ... | @@ -204,19 +227,21 @@ |
204 | View.prototype.build = function() { | 227 | View.prototype.build = function() { |
205 | var bindingRegExp, el, node, parse, skipNodes, _i, _j, _len, _len1, _ref, _ref1, | 228 | var bindingRegExp, el, node, parse, skipNodes, _i, _j, _len, _len1, _ref, _ref1, |
206 | _this = this; | 229 | _this = this; |
230 | |||
207 | this.bindings = []; | 231 | this.bindings = []; |
208 | skipNodes = []; | 232 | skipNodes = []; |
209 | bindingRegExp = this.bindingRegExp(); | 233 | bindingRegExp = this.bindingRegExp(); |
210 | parse = function(node) { | 234 | parse = function(node) { |
211 | var attribute, attributes, binder, binding, context, ctx, dependencies, identifier, keypath, model, n, options, path, pipe, pipes, regexp, splitPath, type, value, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; | 235 | var attribute, attributes, binder, context, ctx, dependencies, identifier, key, keypath, n, options, path, pipe, pipes, regexp, splitPath, type, value, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; |
236 | |||
212 | if (__indexOf.call(skipNodes, node) < 0) { | 237 | if (__indexOf.call(skipNodes, node) < 0) { |
213 | _ref = node.attributes; | 238 | _ref = node.attributes; |
214 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 239 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
215 | attribute = _ref[_i]; | 240 | attribute = _ref[_i]; |
216 | if (bindingRegExp.test(attribute.name)) { | 241 | if (bindingRegExp.test(attribute.name)) { |
217 | type = attribute.name.replace(bindingRegExp, ''); | 242 | type = attribute.name.replace(bindingRegExp, ''); |
218 | if (!(binder = Rivets.binders[type])) { | 243 | if (!(binder = _this.binders[type])) { |
219 | _ref1 = Rivets.binders; | 244 | _ref1 = _this.binders; |
220 | for (identifier in _ref1) { | 245 | for (identifier in _ref1) { |
221 | value = _ref1[identifier]; | 246 | value = _ref1[identifier]; |
222 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | 247 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { |
... | @@ -227,7 +252,7 @@ | ... | @@ -227,7 +252,7 @@ |
227 | } | 252 | } |
228 | } | 253 | } |
229 | } | 254 | } |
230 | binder || (binder = Rivets.binders['*']); | 255 | binder || (binder = _this.binders['*']); |
231 | if (binder.block) { | 256 | if (binder.block) { |
232 | _ref2 = node.getElementsByTagName('*'); | 257 | _ref2 = node.getElementsByTagName('*'); |
233 | for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { | 258 | for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { |
... | @@ -243,12 +268,10 @@ | ... | @@ -243,12 +268,10 @@ |
243 | attribute = _ref3[_k]; | 268 | attribute = _ref3[_k]; |
244 | if (bindingRegExp.test(attribute.name)) { | 269 | if (bindingRegExp.test(attribute.name)) { |
245 | options = {}; | 270 | options = {}; |
246 | if ((_this.options != null) && typeof _this.options === 'object') { | ||
247 | options.bindingOptions = _this.options; | ||
248 | } | ||
249 | type = attribute.name.replace(bindingRegExp, ''); | 271 | type = attribute.name.replace(bindingRegExp, ''); |
250 | pipes = (function() { | 272 | pipes = (function() { |
251 | var _l, _len3, _ref4, _results; | 273 | var _l, _len3, _ref4, _results; |
274 | |||
252 | _ref4 = attribute.value.split('|'); | 275 | _ref4 = attribute.value.split('|'); |
253 | _results = []; | 276 | _results = []; |
254 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { | 277 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { |
... | @@ -259,6 +282,7 @@ | ... | @@ -259,6 +282,7 @@ |
259 | })(); | 282 | })(); |
260 | context = (function() { | 283 | context = (function() { |
261 | var _l, _len3, _ref4, _results; | 284 | var _l, _len3, _ref4, _results; |
285 | |||
262 | _ref4 = pipes.shift().split('<'); | 286 | _ref4 = pipes.shift().split('<'); |
263 | _results = []; | 287 | _results = []; |
264 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { | 288 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { |
... | @@ -272,19 +296,17 @@ | ... | @@ -272,19 +296,17 @@ |
272 | options.formatters = pipes; | 296 | options.formatters = pipes; |
273 | options.bypass = path.indexOf(':') !== -1; | 297 | options.bypass = path.indexOf(':') !== -1; |
274 | if (splitPath[0]) { | 298 | if (splitPath[0]) { |
275 | model = _this.models[splitPath.shift()]; | 299 | key = splitPath.shift(); |
276 | } else { | 300 | } else { |
277 | model = _this.models; | 301 | key = null; |
278 | splitPath.shift(); | 302 | splitPath.shift(); |
279 | } | 303 | } |
280 | keypath = splitPath.join('.'); | 304 | keypath = splitPath.join('.'); |
281 | if (model) { | 305 | if (_this.models[key] != null) { |
282 | if (dependencies = context.shift()) { | 306 | if (dependencies = context.shift()) { |
283 | options.dependencies = dependencies.split(/\s+/); | 307 | options.dependencies = dependencies.split(/\s+/); |
284 | } | 308 | } |
285 | binding = new Rivets.Binding(node, type, model, keypath, options); | 309 | _this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options)); |
286 | binding.view = _this; | ||
287 | _this.bindings.push(binding); | ||
288 | } | 310 | } |
289 | } | 311 | } |
290 | } | 312 | } |
... | @@ -309,6 +331,7 @@ | ... | @@ -309,6 +331,7 @@ |
309 | 331 | ||
310 | View.prototype.select = function(fn) { | 332 | View.prototype.select = function(fn) { |
311 | var binding, _i, _len, _ref, _results; | 333 | var binding, _i, _len, _ref, _results; |
334 | |||
312 | _ref = this.bindings; | 335 | _ref = this.bindings; |
313 | _results = []; | 336 | _results = []; |
314 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 337 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -322,6 +345,7 @@ | ... | @@ -322,6 +345,7 @@ |
322 | 345 | ||
323 | View.prototype.bind = function() { | 346 | View.prototype.bind = function() { |
324 | var binding, _i, _len, _ref, _results; | 347 | var binding, _i, _len, _ref, _results; |
348 | |||
325 | _ref = this.bindings; | 349 | _ref = this.bindings; |
326 | _results = []; | 350 | _results = []; |
327 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 351 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -333,6 +357,7 @@ | ... | @@ -333,6 +357,7 @@ |
333 | 357 | ||
334 | View.prototype.unbind = function() { | 358 | View.prototype.unbind = function() { |
335 | var binding, _i, _len, _ref, _results; | 359 | var binding, _i, _len, _ref, _results; |
360 | |||
336 | _ref = this.bindings; | 361 | _ref = this.bindings; |
337 | _results = []; | 362 | _results = []; |
338 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 363 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -344,6 +369,7 @@ | ... | @@ -344,6 +369,7 @@ |
344 | 369 | ||
345 | View.prototype.sync = function() { | 370 | View.prototype.sync = function() { |
346 | var binding, _i, _len, _ref, _results; | 371 | var binding, _i, _len, _ref, _results; |
372 | |||
347 | _ref = this.bindings; | 373 | _ref = this.bindings; |
348 | _results = []; | 374 | _results = []; |
349 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 375 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -355,6 +381,7 @@ | ... | @@ -355,6 +381,7 @@ |
355 | 381 | ||
356 | View.prototype.publish = function() { | 382 | View.prototype.publish = function() { |
357 | var binding, _i, _len, _ref, _results; | 383 | var binding, _i, _len, _ref, _results; |
384 | |||
358 | _ref = this.select(function(b) { | 385 | _ref = this.select(function(b) { |
359 | return b.binder.publishes; | 386 | return b.binder.publishes; |
360 | }); | 387 | }); |
... | @@ -366,12 +393,40 @@ | ... | @@ -366,12 +393,40 @@ |
366 | return _results; | 393 | return _results; |
367 | }; | 394 | }; |
368 | 395 | ||
396 | View.prototype.update = function(models) { | ||
397 | var binding, key, model, _results; | ||
398 | |||
399 | if (models == null) { | ||
400 | models = {}; | ||
401 | } | ||
402 | _results = []; | ||
403 | for (key in models) { | ||
404 | model = models[key]; | ||
405 | this.models[key] = model; | ||
406 | _results.push((function() { | ||
407 | var _i, _len, _ref, _results1; | ||
408 | |||
409 | _ref = this.select(function(b) { | ||
410 | return b.key === key; | ||
411 | }); | ||
412 | _results1 = []; | ||
413 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
414 | binding = _ref[_i]; | ||
415 | _results1.push(binding.update()); | ||
416 | } | ||
417 | return _results1; | ||
418 | }).call(this)); | ||
419 | } | ||
420 | return _results; | ||
421 | }; | ||
422 | |||
369 | return View; | 423 | return View; |
370 | 424 | ||
371 | })(); | 425 | })(); |
372 | 426 | ||
373 | bindEvent = function(el, event, handler, context) { | 427 | bindEvent = function(el, event, handler, context) { |
374 | var fn; | 428 | var fn; |
429 | |||
375 | fn = function(e) { | 430 | fn = function(e) { |
376 | return handler.call(context, e); | 431 | return handler.call(context, e); |
377 | }; | 432 | }; |
... | @@ -409,6 +464,7 @@ | ... | @@ -409,6 +464,7 @@ |
409 | 464 | ||
410 | getInputValue = function(el) { | 465 | getInputValue = function(el) { |
411 | var o, _i, _len, _results; | 466 | var o, _i, _len, _results; |
467 | |||
412 | if (window.jQuery != null) { | 468 | if (window.jQuery != null) { |
413 | el = jQuery(el); | 469 | el = jQuery(el); |
414 | switch (el[0].type) { | 470 | switch (el[0].type) { |
... | @@ -454,6 +510,7 @@ | ... | @@ -454,6 +510,7 @@ |
454 | }, | 510 | }, |
455 | routine: function(el, value) { | 511 | routine: function(el, value) { |
456 | var _ref; | 512 | var _ref; |
513 | |||
457 | if (el.type === 'radio') { | 514 | if (el.type === 'radio') { |
458 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0); | 515 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0); |
459 | } else { | 516 | } else { |
... | @@ -471,6 +528,7 @@ | ... | @@ -471,6 +528,7 @@ |
471 | }, | 528 | }, |
472 | routine: function(el, value) { | 529 | routine: function(el, value) { |
473 | var _ref; | 530 | var _ref; |
531 | |||
474 | if (el.type === 'radio') { | 532 | if (el.type === 'radio') { |
475 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0); | 533 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0); |
476 | } else { | 534 | } else { |
... | @@ -497,6 +555,7 @@ | ... | @@ -497,6 +555,7 @@ |
497 | }, | 555 | }, |
498 | routine: function(el, value) { | 556 | routine: function(el, value) { |
499 | var o, _i, _len, _ref, _ref1, _ref2, _results; | 557 | var o, _i, _len, _ref, _ref1, _ref2, _results; |
558 | |||
500 | if (window.jQuery != null) { | 559 | if (window.jQuery != null) { |
501 | el = jQuery(el); | 560 | el = jQuery(el); |
502 | if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) { | 561 | if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) { |
... | @@ -537,10 +596,24 @@ | ... | @@ -537,10 +596,24 @@ |
537 | "each-*": { | 596 | "each-*": { |
538 | block: true, | 597 | block: true, |
539 | bind: function(el, collection) { | 598 | bind: function(el, collection) { |
540 | return el.removeAttribute(['data', Rivets.config.prefix, this.type].join('-').replace('--', '-')); | 599 | return el.removeAttribute(['data', this.view.config.prefix, this.type].join('-').replace('--', '-')); |
600 | }, | ||
601 | unbind: function(el, collection) { | ||
602 | var view, _i, _len, _ref, _results; | ||
603 | |||
604 | if (this.iterated != null) { | ||
605 | _ref = this.iterated; | ||
606 | _results = []; | ||
607 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
608 | view = _ref[_i]; | ||
609 | _results.push(view.unbind()); | ||
610 | } | ||
611 | return _results; | ||
612 | } | ||
541 | }, | 613 | }, |
542 | routine: function(el, collection) { | 614 | routine: function(el, collection) { |
543 | var data, e, item, itemEl, m, n, previous, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results; | 615 | var data, e, item, itemEl, m, n, previous, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results; |
616 | |||
544 | if (this.iterated != null) { | 617 | if (this.iterated != null) { |
545 | _ref = this.iterated; | 618 | _ref = this.iterated; |
546 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 619 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -572,7 +645,7 @@ | ... | @@ -572,7 +645,7 @@ |
572 | itemEl = el.cloneNode(true); | 645 | itemEl = el.cloneNode(true); |
573 | previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker; | 646 | previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker; |
574 | this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null); | 647 | this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null); |
575 | view = new Rivets.View(itemEl, data); | 648 | view = new Rivets.View(itemEl, data, this.view.options); |
576 | view.bind(); | 649 | view.bind(); |
577 | _results.push(this.iterated.push(view)); | 650 | _results.push(this.iterated.push(view)); |
578 | } | 651 | } |
... | @@ -582,6 +655,7 @@ | ... | @@ -582,6 +655,7 @@ |
582 | }, | 655 | }, |
583 | "class-*": function(el, value) { | 656 | "class-*": function(el, value) { |
584 | var elClass; | 657 | var elClass; |
658 | |||
585 | elClass = " " + el.className + " "; | 659 | elClass = " " + el.className + " "; |
586 | if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) { | 660 | if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) { |
587 | return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim(); | 661 | return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim(); |
... | @@ -608,6 +682,7 @@ | ... | @@ -608,6 +682,7 @@ |
608 | exports.config = Rivets.config; | 682 | exports.config = Rivets.config; |
609 | exports.configure = function(options) { | 683 | exports.configure = function(options) { |
610 | var property, value; | 684 | var property, value; |
685 | |||
611 | if (options == null) { | 686 | if (options == null) { |
612 | options = {}; | 687 | options = {}; |
613 | } | 688 | } |
... | @@ -618,6 +693,7 @@ | ... | @@ -618,6 +693,7 @@ |
618 | }; | 693 | }; |
619 | return exports.bind = function(el, models, options) { | 694 | return exports.bind = function(el, models, options) { |
620 | var view; | 695 | var view; |
696 | |||
621 | if (models == null) { | 697 | if (models == null) { |
622 | models = {}; | 698 | models = {}; |
623 | } | 699 | } | ... | ... |
dist/rivets.min.js
0 → 100644
1 | // rivets.js | ||
2 | // version: 0.5.0 | ||
3 | // author: Michael Richards | ||
4 | // license: MIT | ||
5 | (function(){var t,i,e,n,s,r=function(t,i){return function(){return t.apply(i,arguments)}},h=[].slice,o=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function t(t,i,e,n,s,h){var o,u,l,a;if(this.view=t,this.el=i,this.type=e,this.key=n,this.keypath=s,this.options=null!=h?h:{},this.update=r(this.update,this),this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.publish=r(this.publish,this),this.sync=r(this.sync,this),this.set=r(this.set,this),this.formattedValue=r(this.formattedValue,this),!(this.binder=this.view.binders[e])){a=this.view.binders;for(o in a)l=a[o],"*"!==o&&-1!==o.indexOf("*")&&(u=RegExp("^"+o.replace("*",".+")+"$"),u.test(e)&&(this.binder=l,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(e),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.view.models[this.key]}return t.prototype.formattedValue=function(t){var i,e,n,s,r,o,u,l,a;for(o=this.formatters,s=0,r=o.length;r>s;s++)e=o[s],i=e.split(/\s+/),n=i.shift(),e=this.model[n]instanceof Function?this.model[n]:(null!=(u=this.options)?null!=(l=u.bindingOptions)?null!=(a=l.formatters)?a[n]:void 0:void 0:void 0)instanceof Function?this.options.bindingOptions.formatters[n]:this.view.formatters[n],(null!=e?e.read:void 0)instanceof Function?t=e.read.apply(e,[t].concat(h.call(i))):e instanceof Function&&(t=e.apply(null,[t].concat(h.call(i))));return t},t.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},t.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},t.prototype.publish=function(){var t,i,e,s,r,o,u,l,a;for(s=n(this.el),u=this.formatters.slice(0).reverse(),r=0,o=u.length;o>r;r++)i=u[r],t=i.split(/\s+/),e=t.shift(),(null!=(l=this.view.formatters[e])?l.publish:void 0)&&(s=(a=this.view.formatters[e]).publish.apply(a,[s].concat(h.call(t))));return this.view.config.adapter.publish(this.model,this.keypath,s)},t.prototype.bind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.subscribe(e,i,this.sync));return u}},t.prototype.unbind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return u}},t.prototype.update=function(){return this.unbind(),this.model=this.view.models[this.key],this.bind()},t}(),t.View=function(){function i(i,e,n){var s,h,o,u,l,a,d,c,f,p;for(this.els=i,this.models=e,this.options=null!=n?n:{},this.update=r(this.update,this),this.publish=r(this.publish,this),this.sync=r(this.sync,this),this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.select=r(this.select,this),this.build=r(this.build,this),this.bindingRegExp=r(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(s in c)o=c[s],this[h][s]=o}f=t[h];for(s in f)o=f[s],null==(p=(u=this[h])[s])&&(u[s]=o)}this.build()}return i.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},i.prototype.build=function(){var i,e,n,s,r,h,u,l,a,d,c,f=this;for(this.bindings=[],r=[],i=this.bindingRegExp(),s=function(e){var n,s,h,u,l,a,d,c,p,b,v,g,y,m,w,x,k,E,j,L,N,Q,R,V,O,B,F,T;if(0>o.call(r,e)){for(O=e.attributes,j=0,Q=O.length;Q>j;j++)if(n=O[j],i.test(n.name)){if(k=n.name.replace(i,""),!(h=f.binders[k])){B=f.binders;for(d in B)E=B[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(k)&&(h=E))}if(h||(h=f.binders["*"]),h.block){for(F=e.getElementsByTagName("*"),L=0,R=F.length;R>L;L++)b=F[L],r.push(b);s=[n]}}for(T=s||e.attributes,N=0,V=T.length;V>N;N++)n=T[N],i.test(n.name)&&(v={},k=n.name.replace(i,""),m=function(){var t,i,e,s;for(e=n.value.split("|"),s=[],t=0,i=e.length;i>t;t++)y=e[t],s.push(y.trim());return s}(),u=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=u.shift(),x=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),x[0]?c=x.shift():(c=null,x.shift()),p=x.join("."),null!=f.models[c]&&((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,k,c,p,v))));s&&(s=null)}},d=this.els,h=0,l=d.length;l>h;h++)for(e=d[h],s(e),c=e.getElementsByTagName("*"),u=0,a=c.length;a>u;u++)n=c[u],null!=n.attributes&&s(n)},i.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},i.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},i.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},i.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},i.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},i.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},i}(),i=function(t,i,e,n){var s;return s=function(t){return e.call(n,t)},null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,s):t.bind(i,s)):null!=window.addEventListener?t.addEventListener(i,s,!1):(i="on"+i,t.attachEvent(i,s)),s},s=function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},n=function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(t){return this.currentListener=i(t,"change",this.publish)},unbind:function(t){return s(t,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(t){return this.currentListener=i(t,"change",this.publish)},unbind:function(t){return s(t,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(t){return this.currentListener=i(t,"change",this.publish)},unbind:function(t){return s(t,"change",this.currentListener)},routine:function(t,i){var e,n,s,r,h,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(r=t.val())?""+r:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],n=0,s=t.length;s>n;n++)e=t[n],l.push(e.selected=(h=e.value,o.call(i,h)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,routine:function(t,e){return this.currentListener&&s(t,this.args[0],this.currentListener),this.currentListener=i(t,this.args[0],e,this.model)}},"each-*":{block:!0,bind:function(t){return t.removeAttribute(["data",this.view.config.prefix,this.type].join("-").replace("--","-"))},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,u,l,a,d,c,f,p,b,v,g,y,m,w,x;if(null!=this.iterated)for(g=this.iterated,d=0,p=g.length;p>d;d++)for(a=g[d],a.unbind(),y=a.els,c=0,b=y.length;b>c;c++)s=y[c],s.parentNode.removeChild(s);else this.marker=document.createComment(" rivets: "+this.type+" "),i.parentNode.insertBefore(this.marker,i),i.parentNode.removeChild(i);if(this.iterated=[],e){for(x=[],f=0,v=e.length;v>f;f++){r=e[f],n={},m=this.view.models;for(u in m)o=m[u],n[u]=o;n[this.args[0]]=r,h=i.cloneNode(!0),l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,this.marker.parentNode.insertBefore(h,null!=(w=l.nextSibling)?w:null),a=new t.View(h,n,this.view.options),a.bind(),x.push(this.iterated.push(a))}return x}}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0},t.formatters={},e=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?e(exports):"function"==typeof define&&define.amd?define(["exports"],function(t){return e(this.rivets=t),t}):e(this.rivets={})}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lib/rivets.min.js
deleted
100644 → 0
1 | // rivets.js | ||
2 | // version: 0.4.9 | ||
3 | // author: Michael Richards | ||
4 | // license: MIT | ||
5 | (function(){var e,t,n,r,i,s=function(e,t){return function(){return e.apply(t,arguments)}},o=[].slice,u=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};e={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),e.Binding=function(){function t(t,n,r,i,o){var u,a,f,l;this.el=t,this.type=n,this.model=r,this.keypath=i,this.options=o!=null?o:{},this.unbind=s(this.unbind,this),this.bind=s(this.bind,this),this.publish=s(this.publish,this),this.sync=s(this.sync,this),this.set=s(this.set,this),this.formattedValue=s(this.formattedValue,this);if(!(this.binder=e.binders[n])){l=e.binders;for(u in l)f=l[u],u!=="*"&&u.indexOf("*")!==-1&&(a=new RegExp("^"+u.replace("*",".+")+"$"),a.test(n)&&(this.binder=f,this.args=(new RegExp("^"+u.replace("*","(.+)")+"$")).exec(n),this.args.shift()))}this.binder||(this.binder=e.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[]}return t.prototype.formattedValue=function(t){var n,r,i,s,u,a,f,l,c;a=this.formatters;for(s=0,u=a.length;s<u;s++)r=a[s],n=r.split(/\s+/),i=n.shift(),r=this.model[i]instanceof Function?this.model[i]:((f=this.options)!=null?(l=f.bindingOptions)!=null?(c=l.formatters)!=null?c[i]:void 0:void 0:void 0)instanceof Function?this.options.bindingOptions.formatters[i]:e.formatters[i],(r!=null?r.read:void 0)instanceof Function?t=r.read.apply(r,[t].concat(o.call(n))):r instanceof Function&&(t=r.apply(null,[t].concat(o.call(n))));return t},t.prototype.set=function(e){var t;return e=e instanceof Function&&!this.binder["function"]?this.formattedValue(e.call(this.model)):this.formattedValue(e),(t=this.binder.routine)!=null?t.call(this,this.el,e):void 0},t.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:e.config.adapter.read(this.model,this.keypath))},t.prototype.publish=function(){var t,n,i,s,u,a,f,l,c;s=r(this.el),f=this.formatters.slice(0).reverse();for(u=0,a=f.length;u<a;u++){n=f[u],t=n.split(/\s+/),i=t.shift();if((l=e.formatters[i])!=null?l.publish:void 0)s=(c=e.formatters[i]).publish.apply(c,[s].concat(o.call(t)))}return e.config.adapter.publish(this.model,this.keypath,s)},t.prototype.bind=function(){var t,n,r,i,s,o,u,a,f;(o=this.binder.bind)!=null&&o.call(this,this.el),this.options.bypass?this.sync():(e.config.adapter.subscribe(this.model,this.keypath,this.sync),e.config.preloadData&&this.sync());if((u=this.options.dependencies)!=null?u.length:void 0){a=this.options.dependencies,f=[];for(i=0,s=a.length;i<s;i++)t=a[i],/^\./.test(t)?(r=this.model,n=t.substr(1)):(t=t.split("."),r=this.view.models[t.shift()],n=t.join(".")),f.push(e.config.adapter.subscribe(r,n,this.sync));return f}},t.prototype.unbind=function(){var t,n,r,i,s,o,u,a,f;(o=this.binder.unbind)!=null&&o.call(this,this.el),this.options.bypass||e.config.adapter.unsubscribe(this.model,this.keypath,this.sync);if((u=this.options.dependencies)!=null?u.length:void 0){a=this.options.dependencies,f=[];for(i=0,s=a.length;i<s;i++)t=a[i],/^\./.test(t)?(r=this.model,n=t.substr(1)):(t=t.split("."),r=this.view.models[t.shift()],n=t.join(".")),f.push(e.config.adapter.unsubscribe(r,n,this.sync));return f}},t}(),e.View=function(){function t(e,t,n){this.els=e,this.models=t,this.options=n,this.publish=s(this.publish,this),this.sync=s(this.sync,this),this.unbind=s(this.unbind,this),this.bind=s(this.bind,this),this.select=s(this.select,this),this.build=s(this.build,this),this.bindingRegExp=s(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),this.build()}return t.prototype.bindingRegExp=function(){var t;return t=e.config.prefix,t?new RegExp("^data-"+t+"-"):/^data-/},t.prototype.build=function(){var t,n,r,i,s,o,a,f,l,c,h,p=this;this.bindings=[],s=[],t=this.bindingRegExp(),i=function(n){var r,i,o,a,f,l,c,h,d,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A,O,M,_,D,P;if(u.call(s,n)<0){M=n.attributes;for(N=0,L=M.length;N<L;N++){r=M[N];if(t.test(r.name)){x=r.name.replace(t,"");if(!(o=e.binders[x])){_=e.binders;for(h in _)T=_[h],h!=="*"&&h.indexOf("*")!==-1&&(E=new RegExp("^"+h.replace("*",".+")+"$"),E.test(x)&&(o=T))}o||(o=e.binders["*"]);if(o.block){D=n.getElementsByTagName("*");for(C=0,A=D.length;C<A;C++)m=D[C],s.push(m);i=[r]}}}P=i||n.attributes;for(k=0,O=P.length;k<O;k++){r=P[k];if(t.test(r.name)){g={},p.options!=null&&typeof p.options=="object"&&(g.bindingOptions=p.options),x=r.name.replace(t,""),w=function(){var e,t,n,i;n=r.value.split("|"),i=[];for(e=0,t=n.length;e<t;e++)b=n[e],i.push(b.trim());return i}(),f=function(){var e,t,n,r;n=w.shift().split("<"),r=[];for(e=0,t=n.length;e<t;e++)l=n[e],r.push(l.trim());return r}(),y=f.shift(),S=y.split(/\.|:/),g.formatters=w,g.bypass=y.indexOf(":")!==-1,S[0]?v=p.models[S.shift()]:(v=p.models,S.shift()),d=S.join(".");if(v){if(c=f.shift())g.dependencies=c.split(/\s+/);a=new e.Binding(n,x,v,d,g),a.view=p,p.bindings.push(a)}}}i&&(i=null)}},c=this.els;for(o=0,f=c.length;o<f;o++){n=c[o],i(n),h=n.getElementsByTagName("*");for(a=0,l=h.length;a<l;a++)r=h[a],r.attributes!=null&&i(r)}},t.prototype.select=function(e){var t,n,r,i,s;i=this.bindings,s=[];for(n=0,r=i.length;n<r;n++)t=i[n],e(t)&&s.push(t);return s},t.prototype.bind=function(){var e,t,n,r,i;r=this.bindings,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.bind());return i},t.prototype.unbind=function(){var e,t,n,r,i;r=this.bindings,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.unbind());return i},t.prototype.sync=function(){var e,t,n,r,i;r=this.bindings,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.sync());return i},t.prototype.publish=function(){var e,t,n,r,i;r=this.select(function(e){return e.binder.publishes}),i=[];for(t=0,n=r.length;t<n;t++)e=r[t],i.push(e.publish());return i},t}(),t=function(e,t,n,r){var i;return i=function(e){return n.call(r,e)},window.jQuery!=null?(e=jQuery(e),e.on!=null?e.on(t,i):e.bind(t,i)):window.addEventListener!=null?e.addEventListener(t,i,!1):(t="on"+t,e.attachEvent(t,i)),i},i=function(e,t,n){return window.jQuery!=null?(e=jQuery(e),e.off!=null?e.off(t,n):e.unbind(t,n)):window.removeEventListener?e.removeEventListener(t,n,!1):(t="on"+t,e.detachEvent(t,n))},r=function(e){var t,n,r,i;if(window.jQuery!=null){e=jQuery(e);switch(e[0].type){case"checkbox":return e.is(":checked");default:return e.val()}}else switch(e.type){case"checkbox":return e.checked;case"select-multiple":i=[];for(n=0,r=e.length;n<r;n++)t=e[n],t.selected&&i.push(t.value);return i;default:return e.value}},e.binders={enabled:function(e,t){return e.disabled=!t},disabled:function(e,t){return e.disabled=!!t},checked:{publishes:!0,bind:function(e){return this.currentListener=t(e,"change",this.publish)},unbind:function(e){return i(e,"change",this.currentListener)},routine:function(e,t){var n;return e.type==="radio"?e.checked=((n=e.value)!=null?n.toString():void 0)===(t!=null?t.toString():void 0):e.checked=!!t}},unchecked:{publishes:!0,bind:function(e){return this.currentListener=t(e,"change",this.publish)},unbind:function(e){return i(e,"change",this.currentListener)},routine:function(e,t){var n;return e.type==="radio"?e.checked=((n=e.value)!=null?n.toString():void 0)!==(t!=null?t.toString():void 0):e.checked=!t}},show:function(e,t){return e.style.display=t?"":"none"},hide:function(e,t){return e.style.display=t?"none":""},html:function(e,t){return e.innerHTML=t!=null?t:""},value:{publishes:!0,bind:function(e){return this.currentListener=t(e,"change",this.publish)},unbind:function(e){return i(e,"change",this.currentListener)},routine:function(e,t){var n,r,i,s,o,a,f;if(window.jQuery!=null){e=jQuery(e);if((t!=null?t.toString():void 0)!==((s=e.val())!=null?s.toString():void 0))return e.val(t!=null?t:"")}else if(e.type==="select-multiple"){if(t!=null){f=[];for(r=0,i=e.length;r<i;r++)n=e[r],f.push(n.selected=(o=n.value,u.call(t,o)>=0));return f}}else if((t!=null?t.toString():void 0)!==((a=e.value)!=null?a.toString():void 0))return e.value=t!=null?t:""}},text:function(e,t){return e.innerText!=null?e.innerText=t!=null?t:"":e.textContent=t!=null?t:""},"on-*":{"function":!0,routine:function(e,n){return this.currentListener&&i(e,this.args[0],this.currentListener),this.currentListener=t(e,this.args[0],n,this.model)}},"each-*":{block:!0,bind:function(t,n){return t.removeAttribute(["data",e.config.prefix,this.type].join("-").replace("--","-"))},routine:function(t,n){var r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E;if(this.iterated!=null){g=this.iterated;for(c=0,d=g.length;c<d;c++){l=g[c],l.unbind(),y=l.els;for(h=0,v=y.length;h<v;h++)i=y[h],i.parentNode.removeChild(i)}}else this.marker=document.createComment(" rivets: "+this.type+" "),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t);this.iterated=[];if(n){E=[];for(p=0,m=n.length;p<m;p++){s=n[p],r={},b=this.view.models;for(a in b)u=b[a],r[a]=u;r[this.args[0]]=s,o=t.cloneNode(!0),f=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,this.marker.parentNode.insertBefore(o,(w=f.nextSibling)!=null?w:null),l=new e.View(o,r),l.bind(),E.push(this.iterated.push(l))}return E}}},"class-*":function(e,t){var n;n=" "+e.className+" ";if(!t==(n.indexOf(" "+this.args[0]+" ")!==-1))return e.className=t?""+e.className+" "+this.args[0]:n.replace(" "+this.args[0]+" "," ").trim()},"*":function(e,t){return t?e.setAttribute(this.type,t):e.removeAttribute(this.type)}},e.config={preloadData:!0},e.formatters={},n=function(t){return t.binders=e.binders,t.formatters=e.formatters,t.config=e.config,t.configure=function(t){var n,r;t==null&&(t={});for(n in t)r=t[n],e.config[n]=r},t.bind=function(t,n,r){var i;return n==null&&(n={}),r==null&&(r={}),i=new e.View(t,n,r),i.bind(),i}},typeof exports=="object"?n(exports):typeof define=="function"&&define.amd?define(["exports"],function(e){return n(this.rivets=e),e}):n(this.rivets={})}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | { | 1 | { |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "description": "Declarative data binding facility.", | 3 | "description": "Declarative data binding facility.", |
4 | "version": "0.4.9", | 4 | "version": "0.5.0", |
5 | "author": "Michael Richards", | 5 | "author": "Michael Richards", |
6 | "url": "http://rivetsjs.com", | 6 | "url": "http://rivetsjs.com", |
7 | "main": "./lib/rivets.js", | 7 | "main": "./dist/rivets.js", |
8 | "licenses": [ | 8 | "licenses": [ |
9 | { | 9 | { |
10 | "type": "MIT", | 10 | "type": "MIT", | ... | ... |
-
Please register or sign in to post a comment