【问题标题】:ExtJs Handling Async CallExtJs 处理异步调用
【发布时间】:2016-04-26 18:59:20
【问题描述】:

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

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

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
        })
    ];
},

【问题讨论】:

    标签: javascript asynchronous extjs extjs3


    【解决方案1】:

    Ext.Button.hide(),从以下版本开始可用:1.1.0

    应该对你有用,除非 3.2.1 中存在重大错误。

    【讨论】:

    • 如果我设置隐藏的真或假,它就可以工作。问题是我必须从数据库中获取值,无论我是否必须隐藏。数据库调用异步运行并在菜单初始化后返回
    • @MuthuAnnamalai 使用hide()函数,你可以在组件初始化后隐藏或显示组件,那么你的问题到底出在哪里?
    • 初始化后,我尝试从数据库访问 this.getMenuActions[0].hidden = 值,但对我不起作用。我真的不知道我在哪里犯错。
    • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。
    • getMenuActions 返回一个配置对象,ExtJS 以某种方式(未在您的代码中显示)派生 组件。一旦基于配置对象创建了这些组件,您就必须更改组件,而不是配置对象。请记住,如果有类似名称的函数可用(即总是更喜欢hide() 而不是hidden = true),则永远不要更改组件的配置选项,因为在后台,可能需要进行其他更改,Sencha 团队应该这样做已经在功能代码中照顾到了。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 2017-11-20
      • 2011-03-18
      • 2011-09-18
      相关资源
      最近更新 更多