代码之家  ›  专栏  ›  技术社区  ›  Andre F

使用detachpress/attachpress更改按钮上的功能

  •  0
  • Andre F  · 技术社区  · 6 年前

    我对我的应用程序有要求,我需要根据工作流的状态更改公共按钮的事件处理程序。

    基本上,我需要更改按下按钮时调用的函数,反之亦然,我希望通过使用事件处理函数来实现这一点 拆压机 附着式压力机

    https://ui5.sap.com/#/api/sap.m.Button/methods/detachPress https://ui5.sap.com/#/api/sap.m.Button/methods/attachPress

    我的按钮(XML视图):

    <Button text="Edit" width="50%" id="_editButtonEmail" press="editPIN"/>
    

    在我的控制器上我想改变功能 编辑引脚 通过 取消编辑pin .

    我试过的一些事情:

        editPIN: function(oControlEvent) {
           //change button
           var editButton = this.getView().byId("_editButtonEmail");
           //detach this function on press
           editButton.detachPress(editButton.mEventRegistry.press[0].fFunction);
           editButton.attachPress(this.cancelEditPIN());
        }
    
        cancelEditPIN: function() {
           //do something else
        }
    

    阿尔索

    editPIN: function(oControlEvent) {
       //change button
       var src = oControlEvent.getSource();
       src.detachPress(this.editPIN());
       src.attachPress(this.cancelEditPIN());
    }
    

    这些似乎都不起作用,如果我检查控制台的功能 编辑引脚 仍然依恋着我 梅文特雷基 新闻事件。

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  4
  •   Marc    6 年前

    没有什么比检查gui文本以确定应该执行什么操作更糟糕的了。

    不同的方法使用两个按钮。一次只能看到一个

    <Button
        text="{i18n>editPIN}"
        visible="{= ${myModel>/State} === 'show' }"
        press="editPIN" />
    <Button
        text="{i18n>editCancelPIN}"
        visible="{= ${myModel>/State} === 'edit' }"
        press="cancelEditPIN" />
    

    在这种情况下 {myModel>/State} 是本地json模型,其中存储工作流的当前状态。


    如果您真的想使用attach/detach方法:它可能不起作用,因为您在将方法作为参数传递给attach/detach时调用了这些方法。例如,试试 src.detachPress(this.editPIN); 而不是 src.detachPress(this.editPIN());

        2
  •  0
  •   Andre F    6 年前

    遵循来自 @Jorg ,我创建了另一个函数 检查针 使用if语句比较按钮中的文本,然后根据该文本触发相应的函数。

    我必须声明我正在使用我的i18n文件向我的视图提供文本,这样无论用户使用什么语言,我的textid都不会改变。

    我的Button:

    <Button text="Edit" width="50%" id="_editButtonEmail" press="checkPIN"/>
    

    我的控制器:

        checkPIN: function(oControlEvent) {
            var src = this.getView().byId("_editButtonEmail").getText();
            var oBundle = this.getView().getModel("i18n").getResourceBundle();
            //call cancelEditPIN
            var editCancelPinText = oBundle.getText("editCancelPIN");
            //call editPIN
            var editPinText = oBundle.getText("editPIN");
            //change button
            if (src === editPinText) {
                this.editPIN(oBundle);
    
            } else if (src === editCancelPinText) {
                this.cancelEditPIN(oBundle);
            }
        },
        editPIN: function(oBundle) {
            //do stuff here
    
            //change button text
            var editButton = this.getView().byId("_editButtonEmail");
            editButton.setText(oBundle.getText("editCancelPIN"));
        },
        cancelEditPIN: function(oBundle) {
            //do different stuff here
    
            //change button text
            var editButton = this.getView().byId("_editButtonEmail");
            editButton.setText(oBundle.getText("editPIN"));
    
        }
    

    不是我想要的答案,因为我想用 拆压机 附着式压力机 所以如果你知道我应该做什么来实现这些,请让我知道。