消除AngularJs指令控制器中的冗余

时间:2014-03-19 11:18:10

标签: javascript angularjs angularjs-directive

我正在寻求改进和简化AngularJs指令控制器

以下是控制器的代码:

 .controller('enhancedZoneCtrl', ['$scope', '$attrs', function ($scope, $attrs) {
        var info = function () {
            var size = 0;
            var infoClass = $attrs.infoClass || '';
            var warnClass = $attrs.warnClass || '';
            var errorClass = $attrs.errorClass || '';
            var minThreshold = $attrs.minThreshold || 3;
            var maxThreshold = $attrs.maxThreshold || 250;
            var tolerance = $attrs.tolerance || 50;
            var initialPrompt = $attrs.initialPrompt || 'enter at least % characters';
            var nToGoPrompt = $attrs.nToGoPrompt || '% more to go...';
            var nLeftPrompt = $attrs.nLeftPrompt || '% characters left';
            var tooLongByPrompt = $attrs.tooLongByPrompt || 'too long by % characters';

            function expandMessage(rawMessage, n) {
                return rawMessage.replace('%', n);
            }

            return {
                getSize: function () {
                    return size;
                },
                setSize: function (newSize) {
                    size = newSize;
                },
                getActiveClass: function () {
                    if (size > maxThreshold - tolerance && size <= maxThreshold) {
                        return warnClass;
                    }
                    else if (size > maxThreshold) {
                        return errorClass;
                    }
                    return infoClass;
                },
                getCurrentMessage: function () {
                    if (size === 0) {
                        return expandMessage(initialPrompt, minThreshold);
                    }
                    else if (size > 0 && size < minThreshold) {
                        return expandMessage(nToGoPrompt, minThreshold - size);
                    }
                    else if(size >= minThreshold && size <= maxThreshold){
                        return expandMessage(nLeftPrompt, maxThreshold -size);
                    }
                    else {
                        return expandMessage(tooLongByPrompt, size - maxThreshold);
                    }
                }
            };
        }();

        var callback;

        this.registerTextChangedCallback = function (callback) {
            this.callback = callback;
        };
        this.notifyObserver = function () {
            this.callback();
        };
        this.setSize = function (size) {
            info.setSize(size);
            this.notifyObserver();
        };
        this.getSize = function () {
            return info.getSize();
        };
        this.getActiveClass = function () {
            return info.getActiveClass();
        };
        this.getCurrentMessage = function () {
            return info.getCurrentMessage();
        };
    }])

我使用立即函数/闭包来隐藏一些变量,但我的代码中有一些冗余,我正在寻求简化指令模块..

我在指令控制器中使用this是否正确?

如何避免指令控制器中的冗余(this.getCurrentMessage调用info.getCurrentMessage)?

指令模块的完整源代码位于here

0 个答案:

没有答案