@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
57 lines (41 loc) • 2.06 kB
text/coffeescript
when 'DOMValue'
if newValue isnt @value or newValue isnt @object.value
@object.value = newValue
@object.dispatchEvent(changeEvent()) if settings.dispatchEvents
when 'DOMRadio'
if @isMultiChoice # The newValue var will hold the radio field binding as its value if the update is coming from the radio field's change event
targetChoiceBinding = if checkIf.isBinding(newValue) then newValue else @choices[newValue]
if targetChoiceBinding
newValue = targetChoiceBinding.object.value
for n,choiceBinding of @choices
choiceBinding.setValue(choiceBinding.ID is targetChoiceBinding.ID, null, publisher)
else
newValue = @value # Set to prev value
else
newValue = !!newValue # Convert to Boolean
return if newValue is @value
@object.checked = newValue unless @object.checked is newValue
@object.dispatchEvent(changeEvent()) if newValue and settings.dispatchEvents # Only emit if the value is true (in order to conform to web standards)
when 'DOMCheckbox'
if @isMultiChoice # The newValue var will hold the checkbox field binding as its value if the update is coming from the checkbox field's change event
overwritePrevious = not checkIf.isBinding(newValue) # Means that a new array was supplied
newChoices = [].concat(newValue) # This *normalizes* the new value into an array
for value,index in newChoices
newChoices[index] = if checkIf.isBinding(value) then value else @choices[value]
newValueArray = []
for choiceName,choiceBinding of @choices
if overwritePrevious
newChoiceValue = targetIncludes(newChoices, choiceBinding)
else
newChoiceValue = choiceBinding.value
choiceBinding.setValue(newChoiceValue, null, publisher)
newValueArray.push(choiceName) if newChoiceValue
newValue = newValueArray
else
newValue = !!newValue # Convert to Boolean
return if newValue is @value
unless @object.checked is newValue
@object.checked = newValue
@object.dispatchEvent(changeEvent()) if settings.dispatchEvents
when 'DOMAttr'
@object.setAttribute(@property, newValue)