2ade95c4 by Adam Heath

Another optimization, reducing rivets.js by 285 bytes.

1 parent 2c19efdb
...@@ -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,29 +334,17 @@ Rivets.binders = ...@@ -325,29 +334,17 @@ 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 338 el.checked = if el.type is 'radio'
330 bind: (el) -> 339 el.value is value
331 @currentListener = bindEvent el, 'change', @publish 340 else
332 unbind: (el) -> 341 !!value
333 unbindEvent el, 'change', @currentListener 342
334 routine: (el, value) -> 343 unchecked: createInputBinder (el, value) ->
335 el.checked = if el.type is 'radio' 344 el.checked = if el.type is 'radio'
336 el.value is value 345 el.value isnt value
337 else 346 else
338 !!value 347 !value
339
340 unchecked:
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'
348 el.value isnt value
349 else
350 !value
351 348
352 show: (el, value) -> 349 show: (el, value) ->
353 el.style.display = if value then '' else 'none' 350 el.style.display = if value then '' else 'none'
...@@ -358,17 +355,11 @@ Rivets.binders = ...@@ -358,17 +355,11 @@ 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 359 if el.type is 'select-multiple'
363 bind: (el) -> 360 o.selected = o.value in value for o in el if value?
364 @currentListener = bindEvent el, 'change', @publish 361 else
365 unbind: (el) -> 362 el.value = if value? then value else ''
366 unbindEvent el, 'change', @currentListener
367 routine: (el, value) ->
368 if el.type is 'select-multiple'
369 o.selected = o.value in value for o in el if value?
370 else
371 el.value = if value? then value else ''
372 363
373 text: (el, value) -> 364 text: (el, value) ->
374 newValue = if value? then value else '' 365 newValue = if value? then value else ''
......