@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
109 lines (70 loc) • 3.23 kB
text/coffeescript
BindingInterfacePrivate =
selfClone: (newStage)-> new BindingInterface(@, newStage)
defineMainProps: (binding)->
= binding
Object.defineProperties @,
'value': get: ()-> binding.value
'original': get: ()-> binding.objects or binding.object
'subscribers': get: ()-> binding.subs.slice().map (sub)-> sub.object
createBinding: (subject, newObjectType, bindingInterface, isSimpleObject)->
= subject
cachedBinding = cache.get(subject, isSimpleObject, , )
if cachedBinding # Exit early by returning the subject from cache if is already in there
return
else
newBinding = new Binding(subject, newObjectType, bindingInterface)
cache.set(newBinding, isSimpleObject)
return newBinding
patchCachedBinding: (cachedBinding)->
cachedBinding.placeholder =
if and not cachedBinding.pholderValues # A cached version with no placeholders set exists - needs scanning
cachedBinding.valueOriginal = cachedBinding.fetchDirectValue()
cachedBinding.scanForPholders()
if cachedBinding.type is 'ObjectProp' and not of # This property was manually deleted and needs its prop to be re-defined as a live one
cachedBinding.convertToLive(true)
if
cachedBinding.optionsDefault[option] = value for option,value of
for key,value of cachedBinding.optionsDefault
[key] = if checkIf.isDefined([key]) then [key] else value
return cachedBinding
setObject: (subject, isSimpleObject)->
import [browserOnly] prototype-private.setObject-parseDOMObject.coffee
switch
when isSimpleObject
newObjectType = if checkIf.isFunction(subject) then 'Func' else 'Array'
when
newObjectType = 'Event'
when
newObjectType = 'Proxy'
import [browserOnly] prototype-private.setObject-setType.DOM.coffee
else
newObjectType = 'ObjectProp'
if is 'multi'
throwError('emptyList') if not subject.length
new GroupBinding(@, subject, newObjectType)
else
if targetIncludes(.type, 'Event') or targetIncludes(.type, 'Proxy')
.updateOnBind = false
else if targetIncludes(.type, 'Func')
.updateOnBind = true
if
return
else
return @
addToPublisher: (publisherInterface)->
if
publisherInterface._.addBinding()
else
publisherInterface.stage = 2
publisherInterface.subs.push(@)
alreadyHadSub = publisherInterface._.addSub(, publisherInterface.options, publisherInterface.updateOnce)
if publisherInterface.updateOnce
delete publisherInterface.updateOnce
else if publisherInterface.options.updateOnBind and not alreadyHadSub
if .isMulti
publisherInterface._.updateSub(binding, publisherInterface._) for binding in .bindings
else
publisherInterface._.updateSub(, publisherInterface._)
return publisherInterface
# BindingInterface:: = Object.create(BindingInterfacePrivate)