0

why can't i use these sap.ui.getCore().setModel(oModel) and this.getView().setModel("oModel","someModel") in functions other than life cycle hooks of controller in sapui5.

I am trying to bind model to the controls of my view without giving it an id. so i want to make a model and set that model to the view where i will bind.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta charset="UTF-8">

        <title>Handler Context in JavaScript Views</title>

        <script id="sap-ui-bootstrap" 
                type="text/javascript"
                src="https://sapui5.netweaver.ondemand.com/resources/sap-ui-core.js"
                data-sap-ui-theme="sap_bluecrystal"
                data-sap-ui-libs="sap.m"
                data-sap-ui-xx-bindingSyntax="complex"
        >
        </script>

        <script>
            jQuery.sap.require("sap.m.MessageToast");
            sap.ui.jsview("local.view", {
                getControllerName: function() {
                    return "local.controller";
                },
                createContent: function(oController) {                    
					var oText=new sap.m.Text();
					oText.bindProperty("text","oModel>/myData");
					
					var oInput=new sap.m.Input();
					oInput.bindValue("oModel>/myData");
					
					var oIn=new sap.m.Input();
					oIn.bindValue("oModel>/myData");
					
					var oBar=new sap.m.Bar({contentLeft:oIn,contentMiddle:oText});
					var oButton=new sap.m.Button({text:"press to reset",
								press:function(){
									oController.resetModel();
								}
								});
					var oPage=new sap.m.Page({headerContent:[oInput],footer:oBar,showNavButton:true,content:[oButton]});
					var oShell=new sap.m.App({pages:oPage});
					return oShell;
                }
            });
			
			
			
            sap.ui.controller("local.controller", {
				onInit:function(){
					var aData={"myData":"enter!!"};
					
					var oModel=new sap.ui.model.json.JSONModel();
					oModel.setData(aData);
					sap.ui.getCore().setModel(oModel,"oModel");
				},
				resetModel:function(){
					var oModel=sap.ui.getCore().getModel("oModel");
					oModel.oData.myData="Reset";				
				}
            });
            sap.ui.view({
                type: sap.ui.core.mvc.ViewType.JS,
                viewName: "local.view"
            }).placeAt("uiArea");
        </script>

    </head>

    <body class="sapUiBody" role="application">
        <div id="uiArea"></div>
    </body>
</html>

how can i update my model which is bound to other controls on button press

Is there any way to access controls in controller of my view without giving it an id?
Regards, Ajaay

3
  • You can definitely use these function calls inside a view or a controller. Maybe this.getView().setModel... is not working because you are inside the wrong context. In other words 'this' might be pointing to an object different than the one you're thinking. Could you please post the code of your view and controller? Commented Nov 20, 2014 at 21:49
  • Thanks fabiopagoti for your reply,i got what you said.:) Commented Nov 21, 2014 at 5:14
  • hey i have the code inserted please check out:) Commented Nov 21, 2014 at 8:16

2 Answers 2

4

You are updating your model incorrectly.

Remember, you should never ever use the internal <yourModel>.oData.<yourProperty> = <whatever>, but instead always use: <yourModel>.setProperty("<propertyName>", <value>). That way you will be absolutely certain your model will be updated with the new values

Change your resetModel method to

resetModel:function(){
    var oModel=sap.ui.getCore().getModel("oModel");
    oModel.setProperty("/myData", "Reset");             
}

and it will work

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Qualiture i got it working.Can you please explain why should not i use <yourModel>.oData.<yourProperty> = <whatever>
If you use <yourModel>.oData.<yourProperty> = <whatever>, you are in essence updating only the internal representation of the model. I.e., the internal values are updated, but this update is unknown to the outside world. When using the setData() or setProperty() methods, this will not only update your model (the oData.<whatever> property will be updated like you have done as well), but depending on the binding type it will also notify any bound controls of the update, and reflect the changes accordingly
Thanks Qualiture for explaining i got it now :)!
1

There are quite a few questions that you have asked here, so let me break it down

  1. Why can't i use these sap.ui.getCore().setModel(oModel) and this.getView().setModel("oModel","someModel") in functions other than life cycle hooks of controller in sapui5. - You can access them

    I'm not sure of where exactly you are facing an issue to use sap.ui.getCore().setModel(oModel), but for this.getView().setModel("oModel","someModel") I can say what @fabiopagoti has commented, that you are probably using it in the wrong context. Here's anexample,

    onInit : function() { /* 'this' refers to the controller here, */ var oController = this; myOwnFunction : function() { /* 'this' now refers to the scope inside 'onInit' Therefore this.getView().setModel() won't work. oController.getView().setModel() will work. */ } }

  2. Is there any way to access controls in controller of my view without giving it an id? - Yes, only if the View is a JavaScript view

    In your view.js instead of declaring a variable as,

    var oVariable;

    Declare it as,

    this.oVariable;

    And anywhere you need to access it in the controller.js (inside the controller's context) access the variable as,

    this.getView().oVariable;

  3. How can i update my model which is bound to other controls on button press

    resetModel:function() { var oModel=sap.ui.getCore().getModel("oModel"); oModel.setProperty("/myData", "Reset" );
    }

The setProperty documentation is here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.