permission.js 987 Bytes
define(['ext/aop', 'zepto'], function (Aop) {
    /**
     * 权限类
     * @param {Object} aop aop设置 advice: 需要进行权限判断的函数名称 namespace: 函数的命名空间
     * @param {Function} judge 权限判断函数 需要返回Boolean或Promise对象 且需要结果为true or flase
     */
    var Permission = function (aop, judge) {
        this.advice = aop.advice;
        this.namespace = aop.namespace;
        this.judge = judge;
    };

    Permission.prototype = {
        init: function () {
            var permission = this;
            
            Aop.around(this.advice, function (f) {
                return $.when(permission.judge.apply(permission.namespace, f.arguments))
                    .then(function (hasPermission) {
                        if (!hasPermission) return $.Deferred().reject();

                        return Aop.next(f);
                    });
            }, this.namespace);
        }
    };

    return Permission;
});