@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
241 lines (122 loc) • 5.31 kB
text/coffeescript
BindingInterface:: = Object.create BindingInterfacePrivate,
of: get: ()-> METHOD_of if not #=== if stage is 0
ofEvent: get: ()-> METHOD_ofEvent if not #=== if stage is 0
chainTo: get: ()-> METHOD_chainTo if #=== if stage is 1 or 2
set: get: ()-> METHOD_set if #=== if stage is 1 or 2
get: get: ()-> METHOD_get if #=== if stage is 1 or 2
transformSelf: get: ()-> METHOD_transformSelf if is 1
transform: get: ()-> METHOD_transform if is 2
transformAll: get: ()-> METHOD_transformAll if is 2
condition: get: ()-> METHOD_condition if is 2
conditionAll: get: ()-> METHOD_conditionAll if is 2
bothWays: get: ()-> METHOD_bothWays if is 2
unBind: get: ()-> METHOD_unBind if is 2
pollEvery: get: ()-> METHOD_pollEvery if is 2
stopPolling: get: ()-> METHOD_stopPolling if is 2
updateSubsOnEvent: get: ()-> METHOD_updateSubsOnEvent if is 2
removeEvent: get: ()-> METHOD_removeEvent if is 2
throttle: get: ()-> METHOD_throttle if #=== if stage is 1 or 2
setOption: get: ()-> METHOD_setOption if is 2
to: get: ()-> genProxiedInterface(@) if is 1
toEvent: get: ()-> genProxiedEventInterface(@) if is 1
and: get: ()->
cloneInterface =
if is 2
return cloneInterface
else if is 1
if not cloneInterface._.isMulti
cloneBinding = cloneInterface._
cloneInterface._ = new GroupBinding(cloneInterface)
cloneInterface._.addBinding(cloneBinding)
return genProxiedInterface(cloneInterface, true)
once: get: ()-> if is 1
interfaceToReturn =
interfaceToReturn.updateOnce = true
return interfaceToReturn
# ==== Aliases =================================================================================
update: get: ()->
twoWay: get: ()->
pipe: get: ()->
METHOD_of = (object)->
throwErrorBadArg(object) unless checkIf.isObject(object) or checkIf.isFunction(object)
if checkIf.isBindingInterface(object)
object = object.object
= 1
return
METHOD_ofEvent = (eventName, customInMethod, customOutMethod)->
if not eventName or not checkIf.isString(eventName)
throwErrorBadArg(eventName)
else if isNaN parseInt()
throwWarning('badEventArg',1)
= eventName
= +'#'+
=
'in': customInMethod
'out': customOutMethod
return @
METHOD_chainTo = (subject, specificOptions, saveOptions)->
return SimplyBind([.length-1]).to(subject, specificOptions, saveOptions)
METHOD_set = (newValue)->
.setValue(newValue, )
return @
METHOD_get = ()->
if not or .isMulti
return .value
else
return .pholderValues[]
METHOD_transformSelf = (transformFn)-> # Applied only to the last sub
if not checkIf.isFunction(transformFn)
throwWarning('fnOnly',1)
else
.setSelfTransform(transformFn, .updateOnBind)
return @
METHOD_transform = (transformFn)-> # Applied only to the last sub
.addModifierFn('transforms', .slice(-1), transformFn, .updateOnBind)
return @
METHOD_transformAll = (transformFn)-> # Applied to entrie subs set
.addModifierFn('transforms', , transformFn, .updateOnBind)
return @
METHOD_condition = (conditionFn)-> # Applied only to the last sub
.addModifierFn('conditions', .slice(-1), conditionFn)
return @
METHOD_conditionAll = (conditionFn)-> # Applied to entrie subs set
.addModifierFn('conditions', , conditionFn)
return @
METHOD_bothWays = (altTransform)-> # Applied only to the last sub
sub = [.length-1] # Last Proxied
subBinding = sub._
bindings = if .isMulti then .bindings else []
subBinding.addSub(, sub.options)
for binding in bindings
originTransform = binding.transforms and binding.transforms[subBinding.ID]
originCondition = binding.conditions and binding.conditions[subBinding.ID]
if originTransform or altTransform
transformToUse = if checkIf.isFunction(altTransform) then altTransform else originTransform
subBinding.setModifierFn(.ID, 'transforms', transformToUse) if transformToUse and altTransform isnt false
if originCondition
subBinding.setModifierFn(.ID, 'conditions', originCondition)
return @
METHOD_unBind = (bothWays)-> # Applied to all subs
.removeSub(sub._, bothWays) for sub in
return @
METHOD_pollEvery = (time)->
.addPollInterval(time)
return @
METHOD_stopPolling = ()->
.removePollInterval()
return @
METHOD_updateSubsOnEvent = (eventName, customMethod)->
.registerEvent(eventName, customMethod)
return @
METHOD_removeEvent = (eventName, customMethod)->
.unRegisterEvent(eventName, customMethod)
return @
METHOD_throttle = (delay)->
if delay and checkIf.isNumber(delay)
.throttleRate = delay
else if delay is false
delete .throttleRate
return @
METHOD_setOption = (optionName, newValue)->
.subOpts[[.length-1]._.ID][optionName] = newValue
return @