Another optimization, reducing rivets.js by 285 bytes.
Showing
1 changed file
with
14 additions
and
23 deletions
... | @@ -68,10 +68,11 @@ class Rivets.Binding | ... | @@ -68,10 +68,11 @@ class Rivets.Binding |
68 | # Syncs up the view binding with the model. | 68 | # Syncs up the view binding with the model. |
69 | sync: => | 69 | sync: => |
70 | keypath = @keypath | 70 | keypath = @keypath |
71 | model = @model | ||
71 | @set if @options.bypass | 72 | @set if @options.bypass |
72 | @model[keypath] | 73 | model[keypath] |
73 | else | 74 | else |
74 | Rivets.config.adapter.read @model, keypath | 75 | Rivets.config.adapter.read model, keypath |
75 | 76 | ||
76 | # Publishes the value currently set on the input element back to the model. | 77 | # Publishes the value currently set on the input element back to the model. |
77 | publish: => | 78 | publish: => |
... | @@ -318,6 +319,14 @@ iterate = (collection, callback) -> | ... | @@ -318,6 +319,14 @@ iterate = (collection, callback) -> |
318 | callback(item, i) for item, i in collection | 319 | callback(item, i) for item, i in collection |
319 | 320 | ||
320 | # Core binding routines. | 321 | # Core binding routines. |
322 | createInputBinder = (routine) -> | ||
323 | publishes: true | ||
324 | bind: (el) -> | ||
325 | @currentListener = bindEvent el, 'change', @publish | ||
326 | unbind: (el) -> | ||
327 | unbindEvent el, 'change', @currentListener | ||
328 | routine: routine | ||
329 | |||
321 | Rivets.binders = | 330 | Rivets.binders = |
322 | enabled: (el, value) -> | 331 | enabled: (el, value) -> |
323 | el.disabled = !value | 332 | el.disabled = !value |
... | @@ -325,25 +334,13 @@ Rivets.binders = | ... | @@ -325,25 +334,13 @@ Rivets.binders = |
325 | disabled: (el, value) -> | 334 | disabled: (el, value) -> |
326 | el.disabled = !!value | 335 | el.disabled = !!value |
327 | 336 | ||
328 | checked: | 337 | checked: createInputBinder (el, value) -> |
329 | publishes: true | ||
330 | bind: (el) -> | ||
331 | @currentListener = bindEvent el, 'change', @publish | ||
332 | unbind: (el) -> | ||
333 | unbindEvent el, 'change', @currentListener | ||
334 | routine: (el, value) -> | ||
335 | el.checked = if el.type is 'radio' | 338 | el.checked = if el.type is 'radio' |
336 | el.value is value | 339 | el.value is value |
337 | else | 340 | else |
338 | !!value | 341 | !!value |
339 | 342 | ||
340 | unchecked: | 343 | unchecked: createInputBinder (el, value) -> |
341 | publishes: true | ||
342 | bind: (el) -> | ||
343 | @currentListener = bindEvent el, 'change', @publish | ||
344 | unbind: (el) -> | ||
345 | unbindEvent el, 'change', @currentListener | ||
346 | routine: (el, value) -> | ||
347 | el.checked = if el.type is 'radio' | 344 | el.checked = if el.type is 'radio' |
348 | el.value isnt value | 345 | el.value isnt value |
349 | else | 346 | else |
... | @@ -358,13 +355,7 @@ Rivets.binders = | ... | @@ -358,13 +355,7 @@ Rivets.binders = |
358 | html: (el, value) -> | 355 | html: (el, value) -> |
359 | el.innerHTML = if value? then value else '' | 356 | el.innerHTML = if value? then value else '' |
360 | 357 | ||
361 | value: | 358 | value: createInputBinder (el, value) -> |
362 | publishes: true | ||
363 | bind: (el) -> | ||
364 | @currentListener = bindEvent el, 'change', @publish | ||
365 | unbind: (el) -> | ||
366 | unbindEvent el, 'change', @currentListener | ||
367 | routine: (el, value) -> | ||
368 | if el.type is 'select-multiple' | 359 | if el.type is 'select-multiple' |
369 | o.selected = o.value in value for o in el if value? | 360 | o.selected = o.value in value for o in el if value? |
370 | else | 361 | else | ... | ... |
-
Please register or sign in to post a comment