Showing posts with label controller. Show all posts
Pass parameters from xml view to function defined in controller

Pass parameters from xml view to function defined in controller

Sanjo Thomas•03:15:00

SAPUI5 is a widely used framework for building web applications. One of the key features of SAPUI5 is the ability to pass parameters to functions from XML views. This allows for greater flexibility and customization in your applications.



To pass parameters to a function from an XML view in SAPUI5, follow these steps:


1. Define your function in your controller. For example, let's say you want to create a function that takes in two parameters, "name" and "age":


```

function myFunction(name, age) {

   // do something with name and age

}

```


2. In your XML view, add a control that will trigger the function. For example, let's say you want to trigger the function when the user clicks a button:


```

<Button text="Click me" press="myFunction"/>

```


3. To pass parameters to the function, use the "data" attribute of the control. For example, let's say you want to pass in the parameters "John" and "30":


```

<Button text="Click me" press="myFunction" data="{name: 'John', age: 30}"/>

```


4. In your controller, use the "getSource" method to get the control that triggered the function. Then use the "data" attribute to get the parameters:


```

function myFunction(event) {

   var button = event.getSource();

   var name = button.data("name");

   var age = button.data("age");

   // do something with name and age

}

```


And that's it! You can now pass parameters to your functions from XML views in SAPUI5. This allows for greater flexibility and customization in your applications, and makes it easier to build complex functionality.

Read more

Use strict in controller.js file

Sanjo Thomas•06:34:00

 

In this blog, we will see the use of "use strict" directive, which is new in ECMAScript version 5.

Lots of developers don’t use "use strict" to ensure their code is running in a secure way. It's important to follow best practices with your code. This article will help you do just that!

The purpose of using strict is to indicate that code should be run in strict mode. For example, an undeclared variable cannot be used in strict mode.

All modern browsers except Internet Explorer 9 and below support "use strict".

                                                             

Declaring ‘’Use strict”

Strict mode is declared by adding "use strict". Beginning of a script or function. Declared at the top of the script and has global scope (all code in the script runs in strict mode).

Example

"use strict";
myRandomFunction();

function myRandomFunction () {
  x = 
9;   // This will also cause an error because x is not declared
}

Declared inside a function, it has local scope (only the code inside the function is in strict mode):

x = 9;       // This will not cause an error.
myFunction();


function myRandomFunction() {
  
"use strict";
  y = 
10;   // This will cause an error
}

 

Why strict mode?

Strict mode makes it easy to write "safe" JavaScript.

Strict mode turns the previously accepted "bad syntax" into a real error. For example, in regular JavaScript, mistyped variable names create new global variables.

Strict mode throws an error, so you can't accidentally create a global variable. In normal JavaScript, assigning a value to a non-writable property does not give the developer an error message. In strict mode, assignment to a non-writable property, getter-only property, non-existent property, variable, or object throws an error.

 

Read more