on 01-05-2015 1:34 PM
Hi,
I used to call and do operations on ui elements by accessing them from controller using below syntax.
this.getView().byId("txtid");
Most of the times , I am able to access UI Elements with above syntax and write validations as well but some times , it is not useful to read panel/tiles by using above syntax.
Are there any known restrictions/limitations in using above syntax?. Are there any alternative ways to call XML View UI Elements from controller,please suggest me.
Regards,
Koti Reddy
My (personal) preference is to *never* access a view element by it's ID if you can avoid it
Although the SAPUI5 framework isn't a strict MVC framework, I try to stick to the MVC principles as much as possible, and this means the Controller should not call/reference items in its View(s).
There are a few exceptions, but generally, using model binding you can almost always bypass the need for referencing controls by their ID's
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I disagree. Even with XML views (I never use any other type of view myself) you can perfectly use validation functionality (either clientside or serverside) without referencing View elements from the controller
Just make sure you set your control's validation methods propery, and set the validation eventhandlers accordingly
Hi Robin,
If I have a simple form and I want to make sure some field will not be blank, how can we validate it in the controller without getting the value from the UI Element?
I am using a PHP server as a backend with the following logic:
- In the view (form) when I click in the "insert" button I call a function (in the controller) that will do a AJAX post in my php, and to validate if all the fields are filled out I have (?) to get one by one (from the UI) in the controller.
Is there a easier/better way to do it? Can you provide us some example?
Thanks a lot!
Cheers,
Eduardo
Hi Eduardo,
Again, use the power of your model
If you need to check if all the fields are filled, you can do it in just a few lines of code, and there is no need -- I repeat, no need -- to get the value of the UI fields one by one:
//assuming your data is an object which is stored in your model as property '/mydata'
var obj = this.getView().getModel().getProperty("/mydata");
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
if(!obj[prop]) { // property is undefined, null, or empty string
// in here, do whatever you need to raise a flag for the empty/null value
}
}
}
I hope you'll see the benefits are obvious: No need to reference View elements (since their ID's may change, are dynamically generated, etc), your code is cleaner and easier to read, etc
No offence to anyone here, but I might write a blog about this anytime soon since it strikes me as odd so many people neglect the power and use cases of model binding
Hi Robin,
I am not offended at all and thanks a lot for your tips!
I am new in the SAPUI5/OpenUI5 world and I am trying to really learn/understand the best practices.
Please check my example here -> http://plnkr.co/edit/aBRvdM
I could not fully understand how to implement your tip in the controller.
Cheers,
Eduardo
Hi Eduardo,
I see you're not using a model right now.
I would advice you to look at chapter Model View Controller first to get a thorough understanding of the theory behind it, and then the chapter about Models and Data Binding which gives code examples and use cases
Hope this helps!
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
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.