openui5,Java脚本 - 嵌套此关键字

时间:2016-02-22 07:09:15

标签: javascript sap sapui5

假设我在JavaScript中使用此代码:

var temp = Controller.extend("sap.ui.demo.wt.controller.App", {
    onInit: function() {
        // set data model on view
        var myoData = {
            recipient: {
                name: "World"
            }
        };
        var oModel = new JSONModel(myoData);
        this.getView().setModel(oModel);
        // set i18n model on view
        var i18nModel = new ResourceModel({
            bundleName: "sap.ui.demo.wt.i18n.i18n"
        });
        this.getView().setModel(i18nModel, "i18n");
    },
    createBtn: function(text) {
        var btn = new sap.m.Button({
            text: text,
            press: function() {
                sap.m.MessageToast.show(this.getText() + ' is clicked');
                var oModel = new JSONModel("resources/products.json"); 
                //** how to call onInit from here ? **
            }
        }).addStyleClass('sapUiSizeCompact');
        return btn;
    }
});

我按照描述写了一个事件按下按钮。 如何在紧急事件中调用onInit函数?

像这样的东西。这个?

谢谢!

1 个答案:

答案 0 :(得分:2)

有几种可能性。我将列出(imo)最常见的那些。

<强> 1。结合

fx:controller="gui.Controller"

<强> 2。该

press: (function (oEvent) {
    // now this refers to your controller, not the sender
    sap.m.MessageToast.show(oEvent.getSource().getText() + ' is clicked');
    var oModel = new JSONModel("resources/products.json");
    this.onInit();
}).bind(this)