UNPKG

@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
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)-> @object = subject cachedBinding = cache.get(subject, isSimpleObject, @selector, @isMultiChoice) if cachedBinding # Exit early by returning the subject from cache if is already in there return @patchCachedBinding(cachedBinding) else newBinding = new Binding(subject, newObjectType, bindingInterface) cache.set(newBinding, isSimpleObject) return newBinding patchCachedBinding: (cachedBinding)-> cachedBinding.placeholder = @placeholder if @placeholder 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 @property not of @object # This property was manually deleted and needs its prop to be re-defined as a live one cachedBinding.convertToLive(true) if @saveOptions cachedBinding.optionsDefault[option] = value for option,value of @optionsPassed for key,value of cachedBinding.optionsDefault @options[key] = if checkIf.isDefined(@optionsPassed[key]) then @optionsPassed[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 @eventName newObjectType = 'Event' when @isProxy newObjectType = 'Proxy' import [browserOnly] prototype-private.setObject-setType.DOM.coffee else newObjectType = 'ObjectProp' if @descriptor is 'multi' throwError('emptyList') if not subject.length @defineMainProps new GroupBinding(@, subject, newObjectType) else @defineMainProps @createBinding(subject, newObjectType, @, isSimpleObject) if targetIncludes(@_.type, 'Event') or targetIncludes(@_.type, 'Proxy') @options.updateOnBind = false else if targetIncludes(@_.type, 'Func') @options.updateOnBind = true if @parentInterface return @addToPublisher(@parentInterface) else return @ addToPublisher: (publisherInterface)-> if @isSibling 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)