on 01-06-2014 11:51 AM
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.
Hi Nandanoor,
You can use below one...
A: function(oEvent){
sap.ui.controller("controllername").B();
};
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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'});
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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 )
Kind regards
dj
Hi Buddha
Start reading code.
this.B();
Cheers
John P
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.