Check in built files.
Showing
2 changed files
with
308 additions
and
244 deletions
... | @@ -3,7 +3,7 @@ | ... | @@ -3,7 +3,7 @@ |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
6 | var Rivets, bindEvent, getInputValue, rivets, unbindEvent, | 6 | var Rivets, bindEvent, convertToModel, createInputBinder, createSubExpressionBinder, defaultExpressionParser, expressionRegex, findBinder, getInputValue, iterate, loopDeps, rivets, unbindEvent, |
7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | 7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, |
8 | __slice = [].slice, | 8 | __slice = [].slice, |
9 | __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | 9 | __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
... | @@ -16,15 +16,39 @@ | ... | @@ -16,15 +16,39 @@ |
16 | }; | 16 | }; |
17 | } | 17 | } |
18 | 18 | ||
19 | findBinder = function(type) { | ||
20 | var args, binder, identifier, regexp, value, _ref; | ||
21 | if (!(binder = Rivets.binders[type])) { | ||
22 | binder = Rivets.binders['*']; | ||
23 | _ref = Rivets.binders; | ||
24 | for (identifier in _ref) { | ||
25 | value = _ref[identifier]; | ||
26 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | ||
27 | regexp = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$"); | ||
28 | if (regexp.test(type)) { | ||
29 | binder = value; | ||
30 | args = regexp.exec(type); | ||
31 | args.shift(); | ||
32 | } | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | if (binder instanceof Function) { | ||
37 | binder = { | ||
38 | routine: binder | ||
39 | }; | ||
40 | } | ||
41 | return [binder, args]; | ||
42 | }; | ||
43 | |||
19 | Rivets.Binding = (function() { | 44 | Rivets.Binding = (function() { |
20 | 45 | ||
21 | function Binding(el, type, model, keypath, options) { | 46 | function Binding(el, type, model, keypath, options) { |
22 | var identifier, regexp, value, _ref; | 47 | var _ref; |
23 | this.el = el; | 48 | this.el = el; |
24 | this.type = type; | 49 | this.type = type; |
25 | this.model = model; | 50 | this.model = model; |
26 | this.keypath = keypath; | 51 | this.keypath = keypath; |
27 | this.options = options != null ? options : {}; | ||
28 | this.unbind = __bind(this.unbind, this); | 52 | this.unbind = __bind(this.unbind, this); |
29 | 53 | ||
30 | this.bind = __bind(this.bind, this); | 54 | this.bind = __bind(this.bind, this); |
... | @@ -37,37 +61,21 @@ | ... | @@ -37,37 +61,21 @@ |
37 | 61 | ||
38 | this.formattedValue = __bind(this.formattedValue, this); | 62 | this.formattedValue = __bind(this.formattedValue, this); |
39 | 63 | ||
40 | if (!(this.binder = Rivets.binders[type])) { | 64 | this.options = (options || (options = {})); |
41 | _ref = Rivets.binders; | 65 | _ref = options.binder ? [options.binder, options.args] : findBinder(type), this.binder = _ref[0], this.args = _ref[1]; |
42 | for (identifier in _ref) { | 66 | this.formatters = options.formatters || []; |
43 | value = _ref[identifier]; | ||
44 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | ||
45 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | ||
46 | if (regexp.test(type)) { | ||
47 | this.binder = value; | ||
48 | this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(type); | ||
49 | this.args.shift(); | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | this.binder || (this.binder = Rivets.binders['*']); | ||
55 | if (this.binder instanceof Function) { | ||
56 | this.binder = { | ||
57 | routine: this.binder | ||
58 | }; | ||
59 | } | ||
60 | this.formatters = this.options.formatters || []; | ||
61 | } | 67 | } |
62 | 68 | ||
63 | Binding.prototype.formattedValue = function(value) { | 69 | Binding.prototype.formattedValue = function(value) { |
64 | var args, formatter, id, _i, _len, _ref; | 70 | var args, formatter, id, m, model, _i, _len, _ref; |
71 | model = this.model; | ||
65 | _ref = this.formatters; | 72 | _ref = this.formatters; |
66 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 73 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
67 | formatter = _ref[_i]; | 74 | formatter = _ref[_i]; |
68 | args = formatter.split(/\s+/); | 75 | args = formatter.split(/\s+/); |
69 | id = args.shift(); | 76 | id = args.shift(); |
70 | formatter = this.model[id] instanceof Function ? this.model[id] : Rivets.formatters[id]; | 77 | m = Rivets.config.adapter.read(model, id); |
78 | formatter = m instanceof Function ? m : Rivets.formatters[id]; | ||
71 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { | 79 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { |
72 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); | 80 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); |
73 | } else if (formatter instanceof Function) { | 81 | } else if (formatter instanceof Function) { |
... | @@ -78,97 +86,205 @@ | ... | @@ -78,97 +86,205 @@ |
78 | }; | 86 | }; |
79 | 87 | ||
80 | Binding.prototype.set = function(value) { | 88 | Binding.prototype.set = function(value) { |
81 | var _ref; | 89 | var binder, _ref; |
82 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); | 90 | binder = this.binder; |
83 | return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0; | 91 | value = this.formattedValue(value instanceof Function && !binder["function"] ? value.call(this.model, this.options.bindContext) : value); |
92 | return (_ref = binder.routine) != null ? _ref.call(this, this.el, value) : void 0; | ||
84 | }; | 93 | }; |
85 | 94 | ||
86 | Binding.prototype.sync = function() { | 95 | Binding.prototype.sync = function() { |
87 | return this.set(this.options.bypass ? this.model[this.keypath] : Rivets.config.adapter.read(this.model, this.keypath)); | 96 | var keypath, model; |
97 | keypath = this.keypath; | ||
98 | model = this.model; | ||
99 | return this.set(this.options.bypass ? model[keypath] : Rivets.config.adapter.read(model, keypath)); | ||
88 | }; | 100 | }; |
89 | 101 | ||
90 | Binding.prototype.publish = function() { | 102 | Binding.prototype.publish = function() { |
91 | var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2; | 103 | var args, f, formatter, id, value, _i, _len, _ref; |
104 | if (this.binder.tokenizes) { | ||
105 | return; | ||
106 | } | ||
92 | value = getInputValue(this.el); | 107 | value = getInputValue(this.el); |
93 | _ref = this.formatters.slice(0).reverse(); | 108 | _ref = this.formatters.slice(0).reverse(); |
94 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 109 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
95 | formatter = _ref[_i]; | 110 | formatter = _ref[_i]; |
96 | args = formatter.split(/\s+/); | 111 | args = formatter.split(/\s+/); |
97 | id = args.shift(); | 112 | id = args.shift(); |
98 | if ((_ref1 = Rivets.formatters[id]) != null ? _ref1.publish : void 0) { | 113 | f = Rivets.formatters[id]; |
99 | value = (_ref2 = Rivets.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); | 114 | if (f != null ? f.publish : void 0) { |
115 | value = f.publish.apply(f, [value].concat(__slice.call(args))); | ||
100 | } | 116 | } |
101 | } | 117 | } |
102 | return Rivets.config.adapter.publish(this.model, this.keypath, value); | 118 | return Rivets.config.adapter.publish(this.model, this.keypath, value); |
103 | }; | 119 | }; |
104 | 120 | ||
105 | Binding.prototype.bind = function() { | 121 | Binding.prototype.bind = function() { |
106 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | 122 | var _ref, |
123 | _this = this; | ||
107 | if ((_ref = this.binder.bind) != null) { | 124 | if ((_ref = this.binder.bind) != null) { |
108 | _ref.call(this, this.el); | 125 | _ref.call(this, this.el); |
109 | } | 126 | } |
110 | if (this.options.bypass) { | 127 | if (this.options.bypass) { |
111 | this.sync(); | 128 | this.sync(); |
112 | } else { | 129 | } else { |
113 | Rivets.config.adapter.subscribe(this.model, this.keypath, this.sync); | 130 | if (this.keypath && !this.binder.tokenizes) { |
131 | Rivets.config.adapter.subscribe(this.model, this.keypath, this.sync); | ||
132 | } | ||
114 | if (Rivets.config.preloadData) { | 133 | if (Rivets.config.preloadData) { |
115 | this.sync(); | 134 | this.sync(); |
116 | } | 135 | } |
117 | } | 136 | } |
118 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | 137 | return loopDeps(this, function(model, keypath) { |
119 | _ref2 = this.options.dependencies; | 138 | return Rivets.config.adapter.subscribe(model, keypath, _this.sync); |
120 | _results = []; | 139 | }); |
121 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
122 | dependency = _ref2[_i]; | ||
123 | if (/^\./.test(dependency)) { | ||
124 | model = this.model; | ||
125 | keypath = dependency.substr(1); | ||
126 | } else { | ||
127 | dependency = dependency.split('.'); | ||
128 | model = this.view.models[dependency.shift()]; | ||
129 | keypath = dependency.join('.'); | ||
130 | } | ||
131 | _results.push(Rivets.config.adapter.subscribe(model, keypath, this.sync)); | ||
132 | } | ||
133 | return _results; | ||
134 | } | ||
135 | }; | 140 | }; |
136 | 141 | ||
137 | Binding.prototype.unbind = function() { | 142 | Binding.prototype.unbind = function() { |
138 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | 143 | var _ref, |
144 | _this = this; | ||
139 | if ((_ref = this.binder.unbind) != null) { | 145 | if ((_ref = this.binder.unbind) != null) { |
140 | _ref.call(this, this.el); | 146 | _ref.call(this, this.el); |
141 | } | 147 | } |
142 | if (!this.options.bypass) { | 148 | if (!(this.options.bypass || !this.binder.tokenizes)) { |
143 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | 149 | if (this.keypath) { |
144 | } | 150 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.sync); |
145 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | ||
146 | _ref2 = this.options.dependencies; | ||
147 | _results = []; | ||
148 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
149 | dependency = _ref2[_i]; | ||
150 | if (/^\./.test(dependency)) { | ||
151 | model = this.model; | ||
152 | keypath = dependency.substr(1); | ||
153 | } else { | ||
154 | dependency = dependency.split('.'); | ||
155 | model = this.view.models[dependency.shift()]; | ||
156 | keypath = dependency.join('.'); | ||
157 | } | ||
158 | _results.push(Rivets.config.adapter.unsubscribe(model, keypath, this.sync)); | ||
159 | } | 151 | } |
160 | return _results; | ||
161 | } | 152 | } |
153 | return loopDeps(this, function(model, keypath) { | ||
154 | return Rivets.config.adapter.unsubscribe(model, keypath, _this.sync); | ||
155 | }); | ||
162 | }; | 156 | }; |
163 | 157 | ||
164 | return Binding; | 158 | return Binding; |
165 | 159 | ||
166 | })(); | 160 | })(); |
167 | 161 | ||
162 | loopDeps = function(binder, callback) { | ||
163 | var dependency, keypath, model, _i, _len, _ref, _ref1, _results; | ||
164 | if ((_ref = binder.options.dependencies) != null ? _ref.length : void 0) { | ||
165 | _ref1 = binder.options.dependencies; | ||
166 | _results = []; | ||
167 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
168 | dependency = _ref1[_i]; | ||
169 | if (/^\./.test(dependency)) { | ||
170 | model = binder.model; | ||
171 | keypath = dependency.substr(1); | ||
172 | } else { | ||
173 | dependency = dependency.split('.'); | ||
174 | model = Rivets.config.adapter.read(binder.view.models(dependency.shift())); | ||
175 | keypath = dependency.join('.'); | ||
176 | } | ||
177 | _results.push(callback(model, keypath)); | ||
178 | } | ||
179 | return _results; | ||
180 | } | ||
181 | }; | ||
182 | |||
183 | expressionRegex = /(.*?)\{\{([^{}]+)\}\}/; | ||
184 | |||
185 | createSubExpressionBinder = function(outerBinding, values, i) { | ||
186 | values[i] = null; | ||
187 | return { | ||
188 | routine: function(el, value) { | ||
189 | values[i] = value; | ||
190 | return outerBinding.sync(); | ||
191 | } | ||
192 | }; | ||
193 | }; | ||
194 | |||
195 | defaultExpressionParser = function(view, node, type, models, value) { | ||
196 | var bindMethod, binder, binderTokenizes, binding, context, ctx, dependencies, firstPart, keypath, matches, model, options, parsingSupport, path, pipe, pipes, splitPath, subBinding, subs, unbindMethod, values, _ref; | ||
197 | if (expressionRegex.test(value)) { | ||
198 | binding = new Rivets.Binding(node, type, models); | ||
199 | values = []; | ||
200 | subs = []; | ||
201 | while (value && expressionRegex.test(value)) { | ||
202 | matches = expressionRegex.exec(value); | ||
203 | value = value.substring(matches[0].length); | ||
204 | if (matches[1]) { | ||
205 | values[values.length] = matches[1]; | ||
206 | } | ||
207 | subs[subs.length] = subBinding = defaultExpressionParser(view, null, '*', models, matches[2]); | ||
208 | subBinding.binder = createSubExpressionBinder(binding, values, values.length); | ||
209 | } | ||
210 | if (value) { | ||
211 | values[values.length] = value; | ||
212 | } | ||
213 | bindMethod = binding.bind; | ||
214 | unbindMethod = binding.unbind; | ||
215 | binding.sync = function() { | ||
216 | return binding.set(values.join('')); | ||
217 | }; | ||
218 | binding.publish = function() {}; | ||
219 | binding.bind = function() { | ||
220 | var sub, _i, _len, _results; | ||
221 | bindMethod(); | ||
222 | _results = []; | ||
223 | for (_i = 0, _len = subs.length; _i < _len; _i++) { | ||
224 | sub = subs[_i]; | ||
225 | _results.push(sub.bind()); | ||
226 | } | ||
227 | return _results; | ||
228 | }; | ||
229 | binding.unbind = function() { | ||
230 | var sub, _i, _len, _results; | ||
231 | unbindMethod(); | ||
232 | _results = []; | ||
233 | for (_i = 0, _len = subs.length; _i < _len; _i++) { | ||
234 | sub = subs[_i]; | ||
235 | _results.push(sub.unbind()); | ||
236 | } | ||
237 | return _results; | ||
238 | }; | ||
239 | return binding; | ||
240 | } | ||
241 | pipes = (function() { | ||
242 | var _i, _len, _ref, _results; | ||
243 | _ref = value.split('|'); | ||
244 | _results = []; | ||
245 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
246 | pipe = _ref[_i]; | ||
247 | _results.push(pipe.trim()); | ||
248 | } | ||
249 | return _results; | ||
250 | })(); | ||
251 | context = (function() { | ||
252 | var _i, _len, _ref, _results; | ||
253 | _ref = pipes.shift().split('<'); | ||
254 | _results = []; | ||
255 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
256 | ctx = _ref[_i]; | ||
257 | _results.push(ctx.trim()); | ||
258 | } | ||
259 | return _results; | ||
260 | })(); | ||
261 | path = context.shift(); | ||
262 | splitPath = path.split(/\.|:/); | ||
263 | options = { | ||
264 | formatters: pipes, | ||
265 | bypass: path.indexOf(':') !== -1, | ||
266 | bindContext: models | ||
267 | }; | ||
268 | parsingSupport = Rivets.config.adapter.parsingSupport; | ||
269 | _ref = findBinder(type), binder = _ref[0], options.args = _ref[1]; | ||
270 | binderTokenizes = binder.tokenizes; | ||
271 | options.binder = binder; | ||
272 | firstPart = parsingSupport || binderTokenizes ? splitPath[0] : splitPath.shift(); | ||
273 | model = firstPart || !binderTokenizes ? Rivets.config.adapter.read(models, firstPart) : models; | ||
274 | keypath = splitPath.join('.'); | ||
275 | if (model || binderTokenizes) { | ||
276 | if (dependencies = context.shift()) { | ||
277 | options.dependencies = dependencies.split(/\s+/); | ||
278 | } | ||
279 | binding = new Rivets.Binding(node, type, (parsingSupport ? models : model), keypath, options); | ||
280 | binding.view = view; | ||
281 | } | ||
282 | return binding; | ||
283 | }; | ||
284 | |||
168 | Rivets.View = (function() { | 285 | Rivets.View = (function() { |
169 | 286 | ||
170 | function View(els, models) { | 287 | function View(els, models) { |
171 | this.els = els; | ||
172 | this.models = models; | 288 | this.models = models; |
173 | this.publish = __bind(this.publish, this); | 289 | this.publish = __bind(this.publish, this); |
174 | 290 | ||
... | @@ -184,9 +300,7 @@ | ... | @@ -184,9 +300,7 @@ |
184 | 300 | ||
185 | this.bindingRegExp = __bind(this.bindingRegExp, this); | 301 | this.bindingRegExp = __bind(this.bindingRegExp, this); |
186 | 302 | ||
187 | if (!(this.els.jquery || this.els instanceof Array)) { | 303 | this.els = els.jquery || els instanceof Array ? els : [els]; |
188 | this.els = [this.els]; | ||
189 | } | ||
190 | this.build(); | 304 | this.build(); |
191 | } | 305 | } |
192 | 306 | ||
... | @@ -201,13 +315,13 @@ | ... | @@ -201,13 +315,13 @@ |
201 | }; | 315 | }; |
202 | 316 | ||
203 | View.prototype.build = function() { | 317 | View.prototype.build = function() { |
204 | var bindingRegExp, el, node, parse, skipNodes, _i, _j, _len, _len1, _ref, _ref1, | 318 | var bindingRegExp, bindings, el, node, parse, skipNodes, _i, _j, _len, _len1, _ref, _ref1, |
205 | _this = this; | 319 | _this = this; |
206 | this.bindings = []; | 320 | bindings = this.bindings = []; |
207 | skipNodes = []; | 321 | skipNodes = []; |
208 | bindingRegExp = this.bindingRegExp(); | 322 | bindingRegExp = this.bindingRegExp(); |
209 | parse = function(node) { | 323 | parse = function(node) { |
210 | 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; | 324 | var attribute, attributes, binder, binding, identifier, n, regexp, type, value, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; |
211 | if (__indexOf.call(skipNodes, node) < 0) { | 325 | if (__indexOf.call(skipNodes, node) < 0) { |
212 | _ref = node.attributes; | 326 | _ref = node.attributes; |
213 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 327 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -241,47 +355,10 @@ | ... | @@ -241,47 +355,10 @@ |
241 | for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { | 355 | for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { |
242 | attribute = _ref3[_k]; | 356 | attribute = _ref3[_k]; |
243 | if (bindingRegExp.test(attribute.name)) { | 357 | if (bindingRegExp.test(attribute.name)) { |
244 | options = {}; | ||
245 | type = attribute.name.replace(bindingRegExp, ''); | 358 | type = attribute.name.replace(bindingRegExp, ''); |
246 | pipes = (function() { | 359 | binding = defaultExpressionParser(_this, node, type, _this.models, attribute.value); |
247 | var _l, _len3, _ref4, _results; | 360 | if (binding) { |
248 | _ref4 = attribute.value.split('|'); | 361 | bindings.push(binding); |
249 | _results = []; | ||
250 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { | ||
251 | pipe = _ref4[_l]; | ||
252 | _results.push(pipe.trim()); | ||
253 | } | ||
254 | return _results; | ||
255 | })(); | ||
256 | context = (function() { | ||
257 | var _l, _len3, _ref4, _results; | ||
258 | _ref4 = pipes.shift().split('<'); | ||
259 | _results = []; | ||
260 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { | ||
261 | ctx = _ref4[_l]; | ||
262 | _results.push(ctx.trim()); | ||
263 | } | ||
264 | return _results; | ||
265 | })(); | ||
266 | path = context.shift(); | ||
267 | splitPath = path.split(/\.|:/); | ||
268 | options.formatters = pipes; | ||
269 | options.bypass = path.indexOf(':') !== -1; | ||
270 | options.bindContext = _this.models; | ||
271 | if (splitPath[0]) { | ||
272 | model = _this.models[splitPath.shift()]; | ||
273 | } else { | ||
274 | model = _this.models; | ||
275 | splitPath.shift(); | ||
276 | } | ||
277 | keypath = splitPath.join('.'); | ||
278 | if (model) { | ||
279 | if (dependencies = context.shift()) { | ||
280 | options.dependencies = dependencies.split(/\s+/); | ||
281 | } | ||
282 | binding = new Rivets.Binding(node, type, model, keypath, options); | ||
283 | binding.view = _this; | ||
284 | _this.bindings.push(binding); | ||
285 | } | 362 | } |
286 | } | 363 | } |
287 | } | 364 | } |
... | @@ -293,7 +370,9 @@ | ... | @@ -293,7 +370,9 @@ |
293 | _ref = this.els; | 370 | _ref = this.els; |
294 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 371 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
295 | el = _ref[_i]; | 372 | el = _ref[_i]; |
296 | parse(el); | 373 | if (el.attributes != null) { |
374 | parse(el); | ||
375 | } | ||
297 | _ref1 = el.getElementsByTagName('*'); | 376 | _ref1 = el.getElementsByTagName('*'); |
298 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | 377 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { |
299 | node = _ref1[_j]; | 378 | node = _ref1[_j]; |
... | @@ -318,49 +397,29 @@ | ... | @@ -318,49 +397,29 @@ |
318 | }; | 397 | }; |
319 | 398 | ||
320 | View.prototype.bind = function() { | 399 | View.prototype.bind = function() { |
321 | var binding, _i, _len, _ref, _results; | 400 | return this.bindings.map(function(binding) { |
322 | _ref = this.bindings; | 401 | return binding.bind(); |
323 | _results = []; | 402 | }); |
324 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
325 | binding = _ref[_i]; | ||
326 | _results.push(binding.bind()); | ||
327 | } | ||
328 | return _results; | ||
329 | }; | 403 | }; |
330 | 404 | ||
331 | View.prototype.unbind = function() { | 405 | View.prototype.unbind = function() { |
332 | var binding, _i, _len, _ref, _results; | 406 | return this.bindings.map(function(binding) { |
333 | _ref = this.bindings; | 407 | return binding.unbind(); |
334 | _results = []; | 408 | }); |
335 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
336 | binding = _ref[_i]; | ||
337 | _results.push(binding.unbind()); | ||
338 | } | ||
339 | return _results; | ||
340 | }; | 409 | }; |
341 | 410 | ||
342 | View.prototype.sync = function() { | 411 | View.prototype.sync = function() { |
343 | var binding, _i, _len, _ref, _results; | 412 | return this.bindings.map(function(binding) { |
344 | _ref = this.bindings; | 413 | return binding.sync(); |
345 | _results = []; | 414 | }); |
346 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
347 | binding = _ref[_i]; | ||
348 | _results.push(binding.sync()); | ||
349 | } | ||
350 | return _results; | ||
351 | }; | 415 | }; |
352 | 416 | ||
353 | View.prototype.publish = function() { | 417 | View.prototype.publish = function() { |
354 | var binding, _i, _len, _ref, _results; | 418 | return (this.select(function(b) { |
355 | _ref = this.select(function(b) { | ||
356 | return b.binder.publishes; | 419 | return b.binder.publishes; |
420 | })).map(function(binding) { | ||
421 | return binding.publish(); | ||
357 | }); | 422 | }); |
358 | _results = []; | ||
359 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
360 | binding = _ref[_i]; | ||
361 | _results.push(binding.publish()); | ||
362 | } | ||
363 | return _results; | ||
364 | }; | 423 | }; |
365 | 424 | ||
366 | return View; | 425 | return View; |
... | @@ -424,30 +483,37 @@ | ... | @@ -424,30 +483,37 @@ |
424 | } | 483 | } |
425 | }; | 484 | }; |
426 | 485 | ||
427 | Rivets.binders = { | 486 | iterate = function(collection, callback) { |
428 | enabled: function(el, value) { | 487 | var i, item, m, n, _i, _len, _results, _results1; |
429 | return el.disabled = !value; | 488 | if (Rivets.config.adapter.iterate) { |
430 | }, | 489 | return Rivets.config.adapter.iterate(collection, callback); |
431 | disabled: function(el, value) { | 490 | } else if (collection instanceof Array) { |
432 | return el.disabled = !!value; | 491 | _results = []; |
433 | }, | 492 | for (i = _i = 0, _len = collection.length; _i < _len; i = ++_i) { |
434 | checked: { | 493 | item = collection[i]; |
435 | publishes: true, | 494 | _results.push(callback(item, i)); |
436 | bind: function(el) { | ||
437 | return this.currentListener = bindEvent(el, 'change', this.publish); | ||
438 | }, | ||
439 | unbind: function(el) { | ||
440 | return unbindEvent(el, 'change', this.currentListener); | ||
441 | }, | ||
442 | routine: function(el, value) { | ||
443 | if (el.type === 'radio') { | ||
444 | return el.checked = el.value === value; | ||
445 | } else { | ||
446 | return el.checked = !!value; | ||
447 | } | ||
448 | } | 495 | } |
449 | }, | 496 | return _results; |
450 | unchecked: { | 497 | } else { |
498 | _results1 = []; | ||
499 | for (n in collection) { | ||
500 | m = collection[n]; | ||
501 | _results1.push(callback(m, n)); | ||
502 | } | ||
503 | return _results1; | ||
504 | } | ||
505 | }; | ||
506 | |||
507 | convertToModel = function(data) { | ||
508 | if (Rivets.config.adapter.convertToModel) { | ||
509 | return Rivets.config.adapter.convertToModel(data); | ||
510 | } else { | ||
511 | return data; | ||
512 | } | ||
513 | }; | ||
514 | |||
515 | createInputBinder = function(routine) { | ||
516 | return { | ||
451 | publishes: true, | 517 | publishes: true, |
452 | bind: function(el) { | 518 | bind: function(el) { |
453 | return this.currentListener = bindEvent(el, 'change', this.publish); | 519 | return this.currentListener = bindEvent(el, 'change', this.publish); |
... | @@ -455,14 +521,23 @@ | ... | @@ -455,14 +521,23 @@ |
455 | unbind: function(el) { | 521 | unbind: function(el) { |
456 | return unbindEvent(el, 'change', this.currentListener); | 522 | return unbindEvent(el, 'change', this.currentListener); |
457 | }, | 523 | }, |
458 | routine: function(el, value) { | 524 | routine: routine |
459 | if (el.type === 'radio') { | 525 | }; |
460 | return el.checked = el.value !== value; | 526 | }; |
461 | } else { | 527 | |
462 | return el.checked = !value; | 528 | Rivets.binders = { |
463 | } | 529 | enabled: function(el, value) { |
464 | } | 530 | return el.disabled = !value; |
465 | }, | 531 | }, |
532 | disabled: function(el, value) { | ||
533 | return el.disabled = !!value; | ||
534 | }, | ||
535 | checked: createInputBinder(function(el, value) { | ||
536 | return el.checked = el.type === 'radio' ? el.value === value : !!value; | ||
537 | }), | ||
538 | unchecked: createInputBinder(function(el, value) { | ||
539 | return el.checked = el.type === 'radio' ? el.value !== value : !value; | ||
540 | }), | ||
466 | show: function(el, value) { | 541 | show: function(el, value) { |
467 | return el.style.display = value ? '' : 'none'; | 542 | return el.style.display = value ? '' : 'none'; |
468 | }, | 543 | }, |
... | @@ -472,44 +547,40 @@ | ... | @@ -472,44 +547,40 @@ |
472 | html: function(el, value) { | 547 | html: function(el, value) { |
473 | return el.innerHTML = value != null ? value : ''; | 548 | return el.innerHTML = value != null ? value : ''; |
474 | }, | 549 | }, |
475 | value: { | 550 | value: createInputBinder(function(el, value) { |
476 | publishes: true, | 551 | var o, _i, _len, _ref, _results; |
477 | bind: function(el) { | 552 | if (el.type === 'select-multiple') { |
478 | return this.currentListener = bindEvent(el, 'change', this.publish); | 553 | if (value != null) { |
479 | }, | 554 | _results = []; |
480 | unbind: function(el) { | 555 | for (_i = 0, _len = el.length; _i < _len; _i++) { |
481 | return unbindEvent(el, 'change', this.currentListener); | 556 | o = el[_i]; |
482 | }, | 557 | _results.push(o.selected = (_ref = o.value, __indexOf.call(value, _ref) >= 0)); |
483 | routine: function(el, value) { | ||
484 | var o, _i, _len, _ref, _results; | ||
485 | if (el.type === 'select-multiple') { | ||
486 | if (value != null) { | ||
487 | _results = []; | ||
488 | for (_i = 0, _len = el.length; _i < _len; _i++) { | ||
489 | o = el[_i]; | ||
490 | _results.push(o.selected = (_ref = o.value, __indexOf.call(value, _ref) >= 0)); | ||
491 | } | ||
492 | return _results; | ||
493 | } | 558 | } |
494 | } else { | 559 | return _results; |
495 | return el.value = value != null ? value : ''; | ||
496 | } | 560 | } |
561 | } else { | ||
562 | return el.value = value != null ? value : ''; | ||
497 | } | 563 | } |
498 | }, | 564 | }), |
499 | text: function(el, value) { | 565 | text: function(el, value) { |
566 | var newValue; | ||
567 | newValue = value != null ? value : ''; | ||
500 | if (el.innerText != null) { | 568 | if (el.innerText != null) { |
501 | return el.innerText = value != null ? value : ''; | 569 | return el.innerText = newValue; |
502 | } else { | 570 | } else { |
503 | return el.textContent = value != null ? value : ''; | 571 | return el.textContent = newValue; |
504 | } | 572 | } |
505 | }, | 573 | }, |
506 | "on-*": { | 574 | "on-*": { |
507 | "function": true, | 575 | "function": true, |
508 | routine: function(el, value) { | 576 | routine: function(el, value) { |
509 | if (this.currentListener) { | 577 | var currentListener, firstArg; |
510 | unbindEvent(el, this.args[0], this.currentListener); | 578 | firstArg = this.args[0]; |
579 | currentListener = this.currentListener; | ||
580 | if (currentListener) { | ||
581 | unbindEvent(el, firstArg, currentListener); | ||
511 | } | 582 | } |
512 | return this.currentListener = bindEvent(el, this.args[0], value, this.model, this.options.bindContext); | 583 | return this.currentListener = bindEvent(el, firstArg, value, this.model, this.options.bindContext); |
513 | } | 584 | } |
514 | }, | 585 | }, |
515 | "each-*": { | 586 | "each-*": { |
... | @@ -518,45 +589,42 @@ | ... | @@ -518,45 +589,42 @@ |
518 | return el.removeAttribute(['data', rivets.config.prefix, this.type].join('-').replace('--', '-')); | 589 | return el.removeAttribute(['data', rivets.config.prefix, this.type].join('-').replace('--', '-')); |
519 | }, | 590 | }, |
520 | routine: function(el, collection) { | 591 | routine: function(el, collection) { |
521 | var data, e, item, itemEl, m, n, previous, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results; | 592 | var e, iterated, marker, parentNode, view, _i, _j, _len, _len1, _ref, |
522 | if (this.iterated != null) { | 593 | _this = this; |
523 | _ref = this.iterated; | 594 | iterated = this.iterated; |
524 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 595 | if (iterated != null) { |
525 | view = _ref[_i]; | 596 | for (_i = 0, _len = iterated.length; _i < _len; _i++) { |
597 | view = iterated[_i]; | ||
526 | view.unbind(); | 598 | view.unbind(); |
527 | _ref1 = view.els; | 599 | _ref = view.els; |
528 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | 600 | for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { |
529 | e = _ref1[_j]; | 601 | e = _ref[_j]; |
530 | e.parentNode.removeChild(e); | 602 | e.parentNode.removeChild(e); |
531 | } | 603 | } |
532 | } | 604 | } |
533 | } else { | 605 | } else { |
534 | this.marker = document.createComment(" rivets: " + this.type + " "); | 606 | marker = this.marker = document.createComment(" rivets: " + this.type + " "); |
535 | el.parentNode.insertBefore(this.marker, el); | 607 | parentNode = el.parentNode; |
536 | el.parentNode.removeChild(el); | 608 | parentNode.insertBefore(marker, el); |
609 | parentNode.removeChild(el); | ||
537 | } | 610 | } |
538 | this.iterated = []; | 611 | this.iterated = iterated = []; |
539 | if (collection) { | 612 | if (collection) { |
540 | _results = []; | 613 | marker = this.marker; |
541 | for (_k = 0, _len2 = collection.length; _k < _len2; _k++) { | 614 | return iterate(collection, function(item, i) { |
542 | item = collection[_k]; | 615 | var data, itemEl, previous, _ref1; |
543 | data = {}; | 616 | data = {}; |
544 | _ref2 = this.view.models; | 617 | iterate(_this.view.models, function(item, i) { |
545 | for (n in _ref2) { | 618 | return data[i] = item; |
546 | m = _ref2[n]; | 619 | }); |
547 | data[n] = m; | 620 | data[_this.args[0]] = item; |
548 | } | 621 | data["" + _this.args[0] + "_index"] = data['rivets_index'] = i; |
549 | data[this.args[0]] = item; | 622 | data = convertToModel(data); |
550 | itemEl = el.cloneNode(true); | 623 | itemEl = el.cloneNode(true); |
551 | if (this.iterated.length > 0) { | 624 | previous = iterated.length > 0 ? iterated[iterated.length - 1].els[0] : marker; |
552 | previous = this.iterated[this.iterated.length - 1].els[0]; | 625 | marker.parentNode.insertBefore(itemEl, (_ref1 = previous.nextSibling) != null ? _ref1 : null); |
553 | } else { | 626 | return iterated.push(rivets.bind(itemEl, data)); |
554 | previous = this.marker; | 627 | }); |
555 | } | ||
556 | this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null); | ||
557 | _results.push(this.iterated.push(rivets.bind(itemEl, data))); | ||
558 | } | ||
559 | return _results; | ||
560 | } | 628 | } |
561 | } | 629 | } |
562 | }, | 630 | }, |
... | @@ -588,9 +656,7 @@ | ... | @@ -588,9 +656,7 @@ |
588 | config: Rivets.config, | 656 | config: Rivets.config, |
589 | configure: function(options) { | 657 | configure: function(options) { |
590 | var property, value; | 658 | var property, value; |
591 | if (options == null) { | 659 | options || (options = {}); |
592 | options = {}; | ||
593 | } | ||
594 | for (property in options) { | 660 | for (property in options) { |
595 | value = options[property]; | 661 | value = options[property]; |
596 | Rivets.config[property] = value; | 662 | Rivets.config[property] = value; |
... | @@ -598,9 +664,7 @@ | ... | @@ -598,9 +664,7 @@ |
598 | }, | 664 | }, |
599 | bind: function(el, models) { | 665 | bind: function(el, models) { |
600 | var view; | 666 | var view; |
601 | if (models == null) { | 667 | models || (models = {}); |
602 | models = {}; | ||
603 | } | ||
604 | view = new Rivets.View(el, models); | 668 | view = new Rivets.View(el, models); |
605 | view.bind(); | 669 | view.bind(); |
606 | return view; | 670 | return view; | ... | ... |
... | @@ -2,4 +2,4 @@ | ... | @@ -2,4 +2,4 @@ |
2 | // version: 0.4.5 | 2 | // version: 0.4.5 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 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;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]: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,r,i,s,u,a,f,l,c;s=n(this.el),f=this.formatters.slice(0).reverse();for(u=0,a=f.length;u<a;u++){r=f[u],t=r.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){this.els=e,this.models=t,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={},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,g.bindContext=p.models,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,i){var s;return s=function(e){return n.call(r,e,i)},window.jQuery!=null?(e=jQuery(e),e.on!=null?e.on(t,s):e.bind(t,s)):window.addEventListener!=null?e.addEventListener(t,s,!1):(t="on"+t,e.attachEvent(t,s)),s},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))},n=function(e){var t,n,r,i;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){return e.type==="radio"?e.checked=e.value===t: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){return e.type==="radio"?e.checked=e.value!==t: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;if(e.type!=="select-multiple")return e.value=t!=null?t:"";if(t!=null){o=[];for(r=0,i=e.length;r<i;r++)n=e[r],o.push(n.selected=(s=n.value,u.call(t,s)>=0));return o}}},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,this.options.bindContext)}},"each-*":{block:!0,bind:function(e,t){return e.removeAttribute(["data",r.config.prefix,this.type].join("-").replace("--","-"))},routine:function(e,t){var n,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+" "),e.parentNode.insertBefore(this.marker,e),e.parentNode.removeChild(e);this.iterated=[];if(t){E=[];for(p=0,m=t.length;p<m;p++){s=t[p],n={},b=this.view.models;for(a in b)u=b[a],n[a]=u;n[this.args[0]]=s,o=e.cloneNode(!0),this.iterated.length>0?f=this.iterated[this.iterated.length-1].els[0]:f=this.marker,this.marker.parentNode.insertBefore(o,(w=f.nextSibling)!=null?w:null),E.push(this.iterated.push(r.bind(o,n)))}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={},r={binders:e.binders,formatters:e.formatters,config:e.config,configure:function(t){var n,r;t==null&&(t={});for(n in t)r=t[n],e.config[n]=r},bind:function(t,n){var r;return n==null&&(n={}),r=new e.View(t,n),r.bind(),r}},typeof module!="undefined"&&module!==null?module.exports=r:this.rivets=r}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
5 | (function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p=function(e,t){return function(){return e.apply(t,arguments)}},d=[].slice,v=[].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,"")}),u=function(t){var n,r,i,s,o,u;if(!(r=e.binders[t])){r=e.binders["*"],u=e.binders;for(i in u)o=u[i],i!=="*"&&i.indexOf("*")!==-1&&(s=new RegExp("^"+i.replace("*","(.+)")+"$"),s.test(t)&&(r=o,n=s.exec(t),n.shift()))}return r instanceof Function&&(r={routine:r}),[r,n]},e.Binding=function(){function t(e,t,n,r,i){var s;this.el=e,this.type=t,this.model=n,this.keypath=r,this.unbind=p(this.unbind,this),this.bind=p(this.bind,this),this.publish=p(this.publish,this),this.sync=p(this.sync,this),this.set=p(this.set,this),this.formattedValue=p(this.formattedValue,this),this.options=i||(i={}),s=i.binder?[i.binder,i.args]:u(t),this.binder=s[0],this.args=s[1],this.formatters=i.formatters||[]}return t.prototype.formattedValue=function(t){var n,r,i,s,o,u,a,f;o=this.model,f=this.formatters;for(u=0,a=f.length;u<a;u++)r=f[u],n=r.split(/\s+/),i=n.shift(),s=e.config.adapter.read(o,i),r=s instanceof Function?s:e.formatters[i],(r!=null?r.read:void 0)instanceof Function?t=r.read.apply(r,[t].concat(d.call(n))):r instanceof Function&&(t=r.apply(null,[t].concat(d.call(n))));return t},t.prototype.set=function(e){var t,n;return t=this.binder,e=this.formattedValue(e instanceof Function&&!t["function"]?e.call(this.model,this.options.bindContext):e),(n=t.routine)!=null?n.call(this,this.el,e):void 0},t.prototype.sync=function(){var t,n;return t=this.keypath,n=this.model,this.set(this.options.bypass?n[t]:e.config.adapter.read(n,t))},t.prototype.publish=function(){var t,n,r,i,s,o,u,f;if(this.binder.tokenizes)return;s=a(this.el),f=this.formatters.slice(0).reverse();for(o=0,u=f.length;o<u;o++){r=f[o],t=r.split(/\s+/),i=t.shift(),n=e.formatters[i];if(n!=null?n.publish:void 0)s=n.publish.apply(n,[s].concat(d.call(t)))}return e.config.adapter.publish(this.model,this.keypath,s)},t.prototype.bind=function(){var t,n=this;return(t=this.binder.bind)!=null&&t.call(this,this.el),this.options.bypass?this.sync():(this.keypath&&!this.binder.tokenizes&&e.config.adapter.subscribe(this.model,this.keypath,this.sync),e.config.preloadData&&this.sync()),l(this,function(t,r){return e.config.adapter.subscribe(t,r,n.sync)})},t.prototype.unbind=function(){var t,n=this;return(t=this.binder.unbind)!=null&&t.call(this,this.el),!this.options.bypass&&!!this.binder.tokenizes&&this.keypath&&e.config.adapter.unsubscribe(this.model,this.keypath,this.sync),l(this,function(t,r){return e.config.adapter.unsubscribe(t,r,n.sync)})},t}(),l=function(t,n){var r,i,s,o,u,a,f,l;if((a=t.options.dependencies)!=null?a.length:void 0){f=t.options.dependencies,l=[];for(o=0,u=f.length;o<u;o++)r=f[o],/^\./.test(r)?(s=t.model,i=r.substr(1)):(r=r.split("."),s=e.config.adapter.read(t.view.models(r.shift())),i=r.join(".")),l.push(n(s,i));return l}},o=/(.*?)\{\{([^{}]+)\}\}/,i=function(e,t,n){return t[n]=null,{routine:function(r,i){return t[n]=i,e.sync()}}},s=function(t,n,r,a,f){var l,c,h,p,d,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A,O,M;if(o.test(f)){p=new e.Binding(n,r,a),O=[],L=[];while(f&&o.test(f))b=o.exec(f),f=f.substring(b[0].length),b[1]&&(O[O.length]=b[1]),L[L.length]=k=s(t,null,"*",a,b[2]),k.binder=i(p,O,O.length);return f&&(O[O.length]=f),l=p.bind,A=p.unbind,p.sync=function(){return p.set(O.join(""))},p.publish=function(){},p.bind=function(){var e,t,n,r;l(),r=[];for(t=0,n=L.length;t<n;t++)e=L[t],r.push(e.bind());return r},p.unbind=function(){var e,t,n,r;A(),r=[];for(t=0,n=L.length;t<n;t++)e=L[t],r.push(e.unbind());return r},p}N=function(){var e,t,n,r;n=f.split("|"),r=[];for(e=0,t=n.length;e<t;e++)T=n[e],r.push(T.trim());return r}(),d=function(){var e,t,n,r;n=N.shift().split("<"),r=[];for(e=0,t=n.length;e<t;e++)v=n[e],r.push(v.trim());return r}(),x=d.shift(),C=x.split(/\.|:/),E={formatters:N,bypass:x.indexOf(":")!==-1,bindContext:a},S=e.config.adapter.parsingSupport,M=u(r),c=M[0],E.args=M[1],h=c.tokenizes,E.binder=c,g=S||h?C[0]:C.shift(),w=g||!h?e.config.adapter.read(a,g):a,y=C.join(".");if(w||h){if(m=d.shift())E.dependencies=m.split(/\s+/);p=new e.Binding(n,r,S?a:w,y,E),p.view=t}return p},e.View=function(){function t(e,t){this.models=t,this.publish=p(this.publish,this),this.sync=p(this.sync,this),this.unbind=p(this.unbind,this),this.bind=p(this.bind,this),this.select=p(this.select,this),this.build=p(this.build,this),this.bindingRegExp=p(this.bindingRegExp,this),this.els=e.jquery||e instanceof Array?e:[e],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,o,u,a,f,l,c,h,p,d=this;n=this.bindings=[],u=[],t=this.bindingRegExp(),o=function(r){var i,o,a,f,l,c,h,p,m,g,y,b,w,E,S,x,T,N,C;if(v.call(u,r)<0){x=r.attributes;for(g=0,w=x.length;g<w;g++){i=x[g];if(t.test(i.name)){p=i.name.replace(t,"");if(!(a=e.binders[p])){T=e.binders;for(l in T)m=T[l],l!=="*"&&l.indexOf("*")!==-1&&(h=new RegExp("^"+l.replace("*",".+")+"$"),h.test(p)&&(a=m))}a||(a=e.binders["*"]);if(a.block){N=r.getElementsByTagName("*");for(y=0,E=N.length;y<E;y++)c=N[y],u.push(c);o=[i]}}}C=o||r.attributes;for(b=0,S=C.length;b<S;b++)i=C[b],t.test(i.name)&&(p=i.name.replace(t,""),f=s(d,r,p,d.models,i.value),f&&n.push(f));o&&(o=null)}},h=this.els;for(a=0,l=h.length;a<l;a++){r=h[a],r.attributes!=null&&o(r),p=r.getElementsByTagName("*");for(f=0,c=p.length;f<c;f++)i=p[f],i.attributes!=null&&o(i)}},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(){return this.bindings.map(function(e){return e.bind()})},t.prototype.unbind=function(){return this.bindings.map(function(e){return e.unbind()})},t.prototype.sync=function(){return this.bindings.map(function(e){return e.sync()})},t.prototype.publish=function(){return this.select(function(e){return e.binder.publishes}).map(function(e){return e.publish()})},t}(),t=function(e,t,n,r,i){var s;return s=function(e){return n.call(r,e,i)},window.jQuery!=null?(e=jQuery(e),e.on!=null?e.on(t,s):e.bind(t,s)):window.addEventListener!=null?e.addEventListener(t,s,!1):(t="on"+t,e.attachEvent(t,s)),s},h=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))},a=function(e){var t,n,r,i;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}},f=function(t,n){var r,i,s,o,u,a,f,l;if(e.config.adapter.iterate)return e.config.adapter.iterate(t,n);if(t instanceof Array){f=[];for(r=u=0,a=t.length;u<a;r=++u)i=t[r],f.push(n(i,r));return f}l=[];for(o in t)s=t[o],l.push(n(s,o));return l},n=function(t){return e.config.adapter.convertToModel?e.config.adapter.convertToModel(t):t},r=function(e){return{publishes:!0,bind:function(e){return this.currentListener=t(e,"change",this.publish)},unbind:function(e){return h(e,"change",this.currentListener)},routine:e}},e.binders={enabled:function(e,t){return e.disabled=!t},disabled:function(e,t){return e.disabled=!!t},checked:r(function(e,t){return e.checked=e.type==="radio"?e.value===t:!!t}),unchecked:r(function(e,t){return e.checked=e.type==="radio"?e.value!==t:!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:r(function(e,t){var n,r,i,s,o;if(e.type!=="select-multiple")return e.value=t!=null?t:"";if(t!=null){o=[];for(r=0,i=e.length;r<i;r++)n=e[r],o.push(n.selected=(s=n.value,v.call(t,s)>=0));return o}}),text:function(e,t){var n;return n=t!=null?t:"",e.innerText!=null?e.innerText=n:e.textContent=n},"on-*":{"function":!0,routine:function(e,n){var r,i;return i=this.args[0],r=this.currentListener,r&&h(e,i,r),this.currentListener=t(e,i,n,this.model,this.options.bindContext)}},"each-*":{block:!0,bind:function(e,t){return e.removeAttribute(["data",c.config.prefix,this.type].join("-").replace("--","-"))},routine:function(e,t){var r,i,s,o,u,a,l,h,p,d,v=this;i=this.iterated;if(i!=null)for(a=0,h=i.length;a<h;a++){u=i[a],u.unbind(),d=u.els;for(l=0,p=d.length;l<p;l++)r=d[l],r.parentNode.removeChild(r)}else s=this.marker=document.createComment(" rivets: "+this.type+" "),o=e.parentNode,o.insertBefore(s,e),o.removeChild(e);this.iterated=i=[];if(t)return s=this.marker,f(t,function(t,r){var o,u,a,l;return o={},f(v.view.models,function(e,t){return o[t]=e}),o[v.args[0]]=t,o[""+v.args[0]+"_index"]=o.rivets_index=r,o=n(o),u=e.cloneNode(!0),a=i.length>0?i[i.length-1].els[0]:s,s.parentNode.insertBefore(u,(l=a.nextSibling)!=null?l:null),i.push(c.bind(u,o))})}},"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={},c={binders:e.binders,formatters:e.formatters,config:e.config,configure:function(t){var n,r;t||(t={});for(n in t)r=t[n],e.config[n]=r},bind:function(t,n){var r;return n||(n={}),r=new e.View(t,n),r.bind(),r}},typeof module!="undefined"&&module!==null?module.exports=c:this.rivets=c}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment