简体   繁体   中英

How to access Model from Controller in OpenUI5 Tutorial Step 7: JSON Model?

In the OpenUI5 Tutorial Step 7:JSON Model , I want to extend the tutorial. If the button is pressed, it should say Hello followed by the name entered in the text box. Could anybody help?

The code in question (provided by the OpenUI5 team) is:

sap.ui.define([
   "sap/ui/core/mvc/Controller",
   "sap/m/MessageToast",
   "sap/ui/model/json/JSONModel"
], function (Controller, MessageToast, JSONModel) {
   "use strict";
   return Controller.extend("sap.ui.demo.wt.controller.App", {
      onInit : function () {
         // set data model on view
         var oData = {
            recipient : {
               name : "World"
            }
         };
         var oModel = new JSONModel(oData);
         this.getView().setModel(oModel);
      },
      onShowHello : function () {
         MessageToast.show("Hello World");
      }
   });
});

In order to access the model from the controller, you would simply do:

onShowHello : function () {
    var oModel = this.getView().getModel(),
        sName = oModel.getProperty("/recipient/name");
    MessageToast.show("Hello, " + sName);
}

@SVM, in the future, please include code in your question so that others with the same problem (or those looking to help) do not have to follow a link that may or may not exist in the future. This may explain your question downvotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM