为什么this.setModel不是函数?

时间:2018-06-14 07:29:06

标签: sapui5

我有以下BaseController定义:

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/resource/ResourceModel"
], (Controller, ResourceModel) => {
    "use strict";

    return Controller.extend("ch.example.northwind.controller.BaseController", {

        /**
         * Convenience method for accessing the router.
         * @public
         * @returns {sap.ui.core.routing.Router} the router for this component
         */
        getRouter: () => {
            return sap.ui.core.UIComponent.getRouterFor(this);
        },

        /**
         * Convenience method for getting the view model by name.
         * @public
         * @param {string} [sName] the model name
         * @returns {sap.ui.model.Model} the model instance
         */
        getModel: (sName) => {
            return this.getView() !== undefined ?
                this.getView().getModel(sName) : this.getOwnerComponent().getModel(sName);
        },

        /**
         * Convenience method for setting the view model.
         * @public
         * @param {sap.ui.model.Model} oModel the model instance
         * @param {string} sName the model name
         * @returns {sap.ui.mvc.View} the view instance
         */
        setModel: (oModel, sName) => {
            return this.getView() !== undefined ?
                this.getView().setModel(oModel, sName) : this.getOwnerComponent().setModel(oModel, sName);
        },

        /**
         * Getter for the resource bundle.
         * @public
         * @returns {sap.ui.model.resource.ResourceModel} the resourceModel of the component
         */
        getI18nBundle: () => {
            return this.getModel("i18n").getResourceBundle();
        },

        /**
         * Read language from the file and is accessible via i18n model. 
         * @param {string} the name the file.
         */
        createI18nModel: (sBundleName) => {
            const oI18n = new ResourceModel({
                bundleName: sBundleName
            });
            this.setModel(oI18n, "i18n");
        }

    });
});  

然后固有的:

sap.ui.define([
    "ch/example/northwind/controller/BaseController",
    "sap/ui/model/resource/ResourceModel",
    "ch/example/northwind/model/models",
    "sap/m/Dialog",
    "sap/m/Text",
    "sap/m/Button"
], (BaseController, ResourceModel, models, Dialog, Text, Button) => {
    "use strict";

    return BaseController.extend("ch.example.northwind.controller.Overview", {
        onInit: function() {
            this.createI18nModel("ch.example.northwind.i18n.overview");
            this.createModels();
            this.queryTotalCustomers();
        },

正如您在上面的代码中看到的那样,我调用了函数this.createI18nModel("ch.example.northwind.i18n.overview");并引发了异常:

BaseController.js?eval:58 Uncaught (in promise) TypeError: this.setModel is not a function
    at f.createI18nModel (BaseController.js?eval:58)
    at f.onInit (Overview.controller.js?eval:13)
    at f.a.fireEvent (EventProvider-dbg.js:228)
    at f.a.fireEvent (Element-dbg.js:431)
    at f.fireAfterInit (ManagedObjectMetadata-dbg.js:568)
    at r (Component-dbg.js:162)
    at f.h.runAsOwner (Component-dbg.js:549)
    at P (View-dbg.js:429)
    at eval (View-dbg.js:467) 

最后一行是抛出异常的点:

createI18nModel: (sBundleName) => {
    const oI18n = new ResourceModel({
        bundleName: sBundleName
    });
    this.setModel(oI18n, "i18n");
}  

考虑this上下文:

enter image description here

它指向窗口对象。

我做错了什么?

0 个答案:

没有答案