cancel
Showing results for 
Search instead for 
Did you mean: 

How to call a function in a controller from different function which is in same controller

Former Member
0 Kudos

Hi,

  In a controller , I have two function 'A' and 'B' , how can we call function 'B' from function 'A'. The normal JS approach is not working for me.

  Sample code:

sap.ui.controller("trailbindingtile.myexample", {

  A: function(oEvent){

alert("in A");

B();  //  this is  not invoking function B. I also tried "sap.ui.getCore().getControl("tileexample").myexample().B()"

  },

B: function(){

alert ("in B");

}

});

Kindly let me know the actual way of implementing this?

Regards

Buddha Puneeth N.

Accepted Solutions (1)

Accepted Solutions (1)

surendra_pamidi
Contributor
0 Kudos

Hi Nandanoor,

You can use below one...

A: function(oEvent){

sap.ui.controller("controllername").B();

};

Former Member
0 Kudos

Hi Surendra,

  I tried that, it is giving error "Uncaught Reference error: B is not defined" at line sap.ui.controller("controllername").B();

  I attached my actual view and it's controller, here error is occurring while calling addNum() function from handleTilePress function.

Thanks & Regards

Buddha Puneeth

surendra_pamidi
Contributor
0 Kudos

Hi Nandanoor,

We can't do declarations in controller.

addNum : function(){

  var imgTemplate = new sap.m.Image({

  src:{

  path:"title",

  formatter:function(element){

  return ('images/'+element+'.png');

  }

  },

  width: '50%',

  height:'50%'

  });

  jsonmodel.getData().push({title:'3'});

  var mytiles = new sap.m.CustomTile({content: imgTemplate,press : addNum()});

  vertbox.bindAggregation("items","/",mytiles);

  vertbox.setModel(jsonmodel);

  }

In the above,

  var imgTemplate = new sap.m.Image({.....................................

  var mytiles = new sap.m.CustomTile({content: imgTemplate,press : addNum()});

You declared in controller. You have to declare in view only and you have to call them to do any functionality from view  by using some ocontroller.someFunction(); like this.....

And another thing is you didn't pass jsonmodel from view to controller...

You can directly write that in view, no need to write in controller i.e.   jsonmodel.getData().push({title:'3'});

surendra_pamidi
Contributor
0 Kudos

Hi Nandanoor,

Or else check whether you put folder name or not if it is needed....



sap.ui.controller("Foldername.controllername").B();

Answers (2)

Answers (2)

qmacro
Developer Advocate
Developer Advocate
0 Kudos

The issue you have is context. A common UI5 pattern is to explicitly provide the context for the event handler when attaching it, and that context is the controller (here, your parent of A and B).

So if in your view you have something like this:

oButton.attachPress(oController.A, oController);

then the context that A will have in "this" is enough to allow you to then write:

alert("in A");

this.B();

and B will get invoked.

If you're using XML views, then this context is passed automatically; you don't need to specify it explicitly. Like this:

<Button text="..." press="A" />

dj

former_member182048
Active Contributor
0 Kudos

I read the article below the other day, its a very good introduction to JavaScript scope, a topic i find very hard to explain.

http://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/

jsp

Former Member
0 Kudos

Hi Adams,

Thanks for your reply, I tried that but it is not working. I enclosed sample code of my view and controller, could you check once.

Thanks & Regards

Buddha Puneeth   

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Hi

Please help us to help you, by creating a Gist with all the relevant files that you have. It's a lot easier for us to reproduce your problem on our local machines, or just browser the entire set of source code files, if it's in a Gist. Much easier than downloading ZIP files containing text files, that we then have to figure out how to put together and make into a complete runnable and debuggable app.

When you do create the Gist, please also include the other files (in this case not just the view and controller, but also the index.html or equivalent).

Create a Gist here: Gists

(although it's not related to SAPUI5, here is an example of a Gist with multiple files: https://gist.github.com/jcla1/7719610 )

UPDATE 09 Jan 2014: See

Kind regards

dj

former_member182048
Active Contributor
0 Kudos

Hi Buddha

Start reading code.


this.B();

Cheers

John P