渲染器方法中的SAPUI5自定义控件调用函数

时间:2016-06-08 06:58:18

标签: javascript sapui5

我对SAPUI5和JavaScript主题不太了解。

目前我正在尝试开发自定义控件。我试图在渲染器方法中调用自定义函数,但在运行时我总是遇到这个错误:

  

错误:未捕获TypeError:this.someFunction不是函数。

我使用SAP的教程代码来说明我的代码的结构。

任何人都可以回答。我非常确定它与JavaScript有关,而与UI5无关。

我的代码如下:

   sap.ui.define([
    "sap/ui/core/Control",
    "sap/m/RatingIndicator",
    "sap/m/Label",
    "sap/m/Button"

], function (Control, RatingIndicator, Label, Button) {
    "use strict";
    return Control.extend("sap.ui.demo.wt.control.ProductRating", {
        metadata : {
            properties : {
                value:  {type : "float", defaultValue : 0}
            },
            aggregations : {
                _rating : {type : "sap.m.RatingIndicator", multiple: false, visibility : "hidden"},
                _label : {type : "sap.m.Label", multiple: false, visibility : "hidden"},
                _button : {type : "sap.m.Button", multiple: false, visibility : "hidden"}
            },
            events : {
                change : {
                    parameters : {
                        value : {type : "int"}
                    }
                }
            }
        },
        init : function () {
            this.setAggregation("_rating", new RatingIndicator({
                value: this.getValue(),
                iconSize: "2rem",
                visualMode: "Half",
                liveChange: this._onRate.bind(this)
            }));
            this.setAggregation("_label", new Label({
                text: "{i18n>productRatingLabelInitial}"
            }).addStyleClass("sapUiTinyMargin"));
            this.setAggregation("_button", new Button({
                text: "{i18n>productRatingButton}",
                press: this._onSubmit.bind(this)
            }));
        },

        setValue: function (iValue) {
            this.setProperty("value", iValue, true);
            this.getAggregation("_rating").setValue(iValue);
        },

        _onRate : function (oEvent) {
            var oRessourceBundle = this.getModel("i18n").getResourceBundle();
            var fValue = oEvent.getParameter("value");

            this.setValue(fValue);

            this.getAggregation("_label").setText(oRessourceBundle.getText("productRatingLabelIndicator", [fValue, oEvent.getSource().getMaxValue()]));
            this.getAggregation("_label").setDesign("Bold");
        },

        _onSubmit : function (oEvent) {
            var oResourceBundle = this.getModel("i18n").getResourceBundle();

            this.getAggregation("_rating").setEnabled(false);
            this.getAggregation("_label").setText(oResourceBundle.getText("productRatingLabelFinal"));
            this.getAggregation("_button").setEnabled(false);
            this.fireEvent("change", {
                value: this.getValue()
            });
        },
        renderer : function (oRM, oControl) {
            oRM.write("<div");
            oRM.writeControlData(oControl);
            oRM.addClass("myAppDemoWTProductRating");
            oRM.writeClasses();
            oRM.write(">");
            oRM.renderControl(oControl.getAggregation("_rating"));
            oRM.renderControl(oControl.getAggregation("_label"));
            oRM.renderControl(oControl.getAggregation("_button"));
            oRM.write("</div>");

            this.someFunction();
        },

        someFunction: function(){
            //Do something

        }


    });
});

1 个答案:

答案 0 :(得分:1)

控制非常好,功能也很好。 但是,我认为,由于您提供var user = new User() { Id = Guid.NewGuid(), DateCreated = DateTime.UtcNow, StateId = STATES.NEW.ID }; 函数,public Map<Integer, Integer> combine(List<Map<Integer, Integer>> maps) { Map<Integer, Integer> result = new HashMap<Integer, Integer>(); for (Map<Integer, Integer> map : maps) { for (Map.Entry<Integer, Integer> entry : map.entrySet()) { int newValue = entry.getValue(); Integer existingValue = result.get(entry.getKey()); if (existingValue != null) { newValue = newValue + existingValue; } result.put(entry.getKey(), newValue); } } return result; } 上下文不再在控件本身中;你应该使用renderer代替。

因此,如果您使用这样的代码,您的代码应该可以使用:

this