0

I am trying to pass element ID as one of the function's parameters:

sap.ui.getCore().byId("idView1").getController().addField("selectedFieldsContainer", oItem);

The definition of the addField function is as follows:

addField: function(sId, oItem){
    var oSelectedFieldsContainer = sap.ui.getCore().byId(sId);
    oSelectedFieldsContainer.addItem(oItem);
}

When I run the code, I get error:

Uncaught TypeError: Cannot read property 'addItem' of undefined

But if I try to explicitly define the id:

sap.ui.getCore().byId("idView1").getController().addField(oItem);

while the function's definition is:

addField: function(oItem){
    var oSelectedFieldsContainer = sap.ui.getCore().byId("selectedFieldsContainer");
    oSelectedFieldsContainer.addItem(oItem);
}

the code works.

I don't understand why the first example doesn't work.

What am I missing?

Thank you.

UPDATE

HERE is JSBIN. I want to update control's type. I try to pass this control's id as a parameter, but sap.ui.getCore().byId() can't find it (see console message).

keshet
  • 1,716
  • 5
  • 27
  • 54
  • can you please provide a jsbin example?. Can you also elaborate on your folder structure. Like details about the folder structure for your app. Also the console value oSelectedFieldsContainer in the first when ID is being to the function? – Veeraraghavan N Aug 31 '15 at 07:20
  • @Veeraraghavan the folder structure is very simple - all functions are in the controller, one after another, separated by comma. oSelectedFieldsContainer is a VBox container to which I want to add new control. – keshet Aug 31 '15 at 07:47

3 Answers3

0

You should know that calling

sap.ui.getCore().byId("control")

does not return a String. From the variable name sId I can guess that you were expecting to receive a String. Instead it returns the control with the given id. Then because of this your changeType() function does not work. Either you pass a reference to the found control to your changeType() function or you pass the string sap.ui.getCore().byId(sId). The jsbin passes the found control instead of the id. Passing the id string would be easy as well...

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

        <title>Example</title>

        <script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" 
            id="sap-ui-bootstrap"
            data-sap-ui-libs="sap.m,sap.ui.layout" 
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-bindingSyntax="complex"
            data-sap-ui-compatVersion="edge"
            data-sap-ui-preload="sync"></script>


        <script type="text/javascript">

            function changeType(oControl, sType){
                oControl.setType(sType);
            }

            var oButton = new sap.m.Button({
                text: "Update Control Type",
                press: function(){
                    var oControl = sap.ui.getCore().byId("control");
                    var sType = "Password";
                    changeType(oControl, sType);
                }
            });

            var oItem = new sap.m.Input("control");
            new sap.m.HBox({
                items: [oButton, oItem]
            }).placeAt("content");

        </script>

    </head>
    <body id="content" class="sapUiBody">
    </body>
</html>
Nabi
  • 2,536
  • 1
  • 16
  • 18
0

Sorry, everyone.

The problem was hiding elsewhere.

Please see the answer to this question.

Community
  • 1
  • 1
keshet
  • 1,716
  • 5
  • 27
  • 54
-1

Here is the working edit Fixed.
The issue is you are passing the object reference in sId field whereas getCore() expects a string. sId.sId get the id of the control you are passing and this appears to work.

Veeraraghavan N
  • 839
  • 1
  • 10
  • 19