rivets-error-binder.js 2.92 KB
define([
    'module',
    'rivets',
], function(
    module,
    rivets
) {
    'use strict';
    var rivetsBinderCall = function(binding, binderName, methodName, args) {
        var binder = rivets.binders[binderName];
        if (binder instanceof Function) {
            if (methodName === 'routine') {
                binder.apply(binding, args);
            }
        } else if (binder) {
            if (binder[methodName]) {
                binder[methodName].apply(binding, args);
            }
        }
    };

    var render = function() {
        var renderImpl = module.config().render;
        if (renderImpl) {
            return renderImpl.apply(this, arguments);
        }
    };

    rivets.binders['error-*'] = {
        bind: function(el) {
            var holder = this.validationHolder = {
                //marker: el.parentNode.insertBefore(document.createComment(" rivets: " + this.type + " "), el),
                focus: function() {
                    render(el, 'focus', false);
                },
                blur: function() {
                    if (holder.observer && holder.observer.target) {
                        holder.observer.target.seen(holder.observer.key.path, true);
                    }
                    render(el, 'blur', false);
                    if (holder.observer && holder.observer.target) {
                        holder.observer.target.validate();
                    }
                },
                validated: function(isValid, model, errors) {
                    var errorList = errors[holder.observer.key.path];
                    if (errorList && holder.observer.target && holder.observer.target.seen(holder.observer.key.path)) {
                        render(el, 'validated', errorList);
                    } else {
                        render(el, 'validated', false);
                    }
                }
            };
            $(el).on('focus', holder.focus).on('blur', holder.blur);
            rivetsBinderCall(this, this.args[0], 'bind', arguments);
        },
        unbind: function(el) {
            var holder = this.validationHolder;
            $(this.validationHolder.marker).after(el).remove();
            $(el).off('focus', holder.focus).off('blur', holder.blur);
            if (holder.observer.target) {
                holder.observer.target.off('validated', holder.validated);
            }
            delete this.validationHolder;
            rivetsBinderCall(this, this.args[0], 'unbind', arguments);
        },
        routine: function() {
            var holder = this.validationHolder;
            if (holder.observer) {
                holder.observer.target.off('validated', holder.validated);
            }
            holder.observer = this.observer;
            if (this.observer.target) {
                this.observer.target.on('validated', holder.validated);
            }
            rivetsBinderCall(this, this.args[0], 'routine', arguments);
        }
    };
});