ExtJs 处理异步调用

ExtJs Handling Async Call

我们正在使用 extjs 3.2.1 JavaScript 框架。在其中一个网格面板的顶部有一个操作菜单。根据从数据库中检索到的值,我需要显示或隐藏菜单。为此,我可以使用菜单的隐藏 属性。

问题是我用来从数据库中检索值的函数异步运行并且需要时间来检索值,到它时 returns 菜单已经初始化。两种方法都在这里。

employeeProfile: function(profileType) {
    CR.Controllers.Employee.GetProfile(function(result) {
        if (CR.CRError.ParseResult(result)) {
            switch (profileType) {
                case "IsStandardAllowed":
                    return result.IsStandardAllowed === 1 ? true : false;
                case "IsUploadAllowed":
                    return result.IsUploadAllowed === 1 ? true : false;
                case "IsCopyAllowed":
                    return result.IsCopyAllowed === 1 ? true : false;
                default:
                    return true;
            }
        }
        return true;
    }, this);
},

getMenuActions:
function() {
    return [
        // add button
        new CR.EmployeePanelAction({
            text: this.lang_newFromStandard,
            tooltip: this.lang_tipNewFromStandard,
            handler: this.onNewFromTemplate,
            hidden: this.EmployeeProfile("IsStandardAllowed")
            scope: this
        }),
        new CR.EmployeePanelAction({
            text: this.lang_newFromUpload,
            tooltip: this.lang_tipNewFromUpload,
            handler: this.onNewFromUpload,
            hidden: this.employeeProfile("IsUploadAllowed"),
            scope: this
        }),
        new CR.EmployeePanelAction({
            text: this.lang_newFromCopy,
            tooltip: this.lang_tipNewFromCopy,
            handler: this.onNewFromCopy,
            hidden: this.employeeProfile("IsCopyAllowed"),
            scope: this
        })
    ];
},

Ext.Button.hide(),可用自:1.1.0

应该对你有效,除非 3.2.1 中存在破坏性错误。

问题是我不明白JavaScript回调和作用域。我将回调方法与范围一起传递给数据库检索方法,它工作正常。这是我用来成功检索的方法。

isImageActionAllowed: function(setImageActions, scope) {
        MVC.Controllers.Users.GetUserProfile(function(result) {
            if(MVC.Error.ParseResult(result)) {
                setImageActions.call(scope, result);
            }
        }, this);
    },