I am a newbie with a simple problem. I am trying to request a focus on a field. Per the FMP → Servoy developers manual it gives the following as an example:
elements.firstName.requestFocus();
But when I try it I get the following error:
ConversionError: The undefined value has no properties. (focus, line2)
As a form element it must be referred to by its property name. In design (layout) mode select the field and look at the Properties list at right. Locate the ‘Name’ property. To the right of it type in the field’s name. Then whenever you refer to it as a form element refer to it by that name. The element name does not have to be the same as the field name. It is just the name you will use to refer to that form element object, per the Servoy object model (SOM).
so… why isn’t the NAME property for fields filled in by default with the name of the DataProvider, so it can be easily used with requestFocus() etc. but still easily changed if needs be.
It took me quite a while to track this down, as the cause of a bug in one of my methods
antonio:
so… why isn’t the NAME property for fields filled in by default with the name of the DataProvider, so it can be easily used with requestFocus() etc. but still easily changed if needs be.
It’s one just one click when placing your field on the form, just select “fill name property” and done!. Usually you do not need to name all the elemets…so I personally agree with servoy way of thinking… “just name it when needed”..
If you select the ‘Name’ when choosing dataproviders for a form, you’ll find that the next time you come back to Developer and select dataproviders for a form that the ‘Name’ property is actually checked. However as Enrico says you’ll find that as you go on in your Servoy development you become more selective in terms of when you check the ‘name’ property.
There are a number of reasons for this. For instance, sometimes you will have many dataproviders on a form and even though you only use the ‘name’ property of a few you have to scroll through them all in a method to find the one you want. This can be a pain. Another thing that occurs, is you will have a ‘for’ statement that will loop through ALL the named elements on a form looking for a particular match and you want Servoy to be able to do that looping as quickly as possible. In general it is very easy to go back and name the few elements that you need on a particular form but it can be a pain to go back and ‘unname’ all the elements you don’t need and you named ‘automatically’. Plus when you do go back and eliminate names, it is often very difficult to remember for sure which ones must continue to be named because they are used in some other method. On the other hand, after a bit you will find it very easy to remember that you have to name an element before you can access its properties like ‘requestFocus’, etc.
John, thanks for the detailed explanation, this sort of advice is very useful. On that topic, I notice that servoy allows the same name to be used on more than one element, which seems like a bad thing.
I thought I should avoid it, however I’ve found I can use it to advantage by setting the same name property to a label/button and also a field. Then I can use application.getMethodTriggerElementName() in a script, eg to trigger a sort by column name in list view, or table view where there’s a header present, call this code with onAction from any column label.
From those with more experience in Servoy, will I regret this shortcut?
I suppose I could consistently apply a suffix to the label names eg 'business_name_column
and change the mehtod to strip ‘_column’ off the application.getMethodTriggerElementName() to get the name of the field to sort.
I’m trying to think of where you might get unstuck with that. I know Servoy stores the name as a combination of the element and the form that it is on (and probably the solution as well). I don’t think though that Servoy stores the type of element (label, button, dataprovider or whatever) but I could be wrong about that. Off the top of my head the only thing I can think of where you might come unstuck is if you/your users want to call/access one of the elements specifically. Say you want to gain focus in a field but not the label. If Servoy doesn’t actually differentiate with a full address including the element type, then I imagine either the method would object (undefined element) or it would act erratically. I suppose you could test it. But somehow I think you might be better off ‘matching’ names with the first part as you outlined. I do that with some methods where I loop through all the elements looking for things named tabpanel or portal at the beginning of the name.
I think naming elements with the same name should not be done!
You are going to rely on it, maybe forget what you exactly did and spend hours and hours debugging something you shouldn’t have done in the first place.