New "controller" usage is giving me warnings

Hi there,

I have a form that manages the CRUD functions.

For example, for delete and create I am using:

/**
 * @properties={typeid:24,uuid:"EB360191-97E7-432F-830F-CAF0F3816298"}
 */
function deleteRecord(event) 
{
	var frm = forms[application.getActiveWindow().controller.getName()];
	frm.deleteRecord(event);
	frm.showAllRecords(event);
}

/**
 * @properties={typeid:24,uuid:"E0768F05-0D74-4895-BB5B-5485D926C8CC"}
 */
function addRecord(event) 
{
	var frm = forms[application.getActiveWindow().controller.getName()];
	frm.addRecord(event);
	frm.setTags_toAdd(event);
}

As you can see, to get the form where the function that I want to trigger resides, I am not using the old currentcontroller (which has been deprecated). I am using the new way: application.getActiveWindow().controller

Still, Servoy developer is showing me warnings.

What am I missing? I tried by looking in how Servoy does it in the example CRM, but Servoy hasn’t updated that solution yet.

Any help on this regard will be appreciated.

Thanks,
JC

Warnings:

[attachment=0]Capture.JPG[/attachment]

this has nothing todo with controller or currentcontroller

You just talk to a generic RuntimeForm if you do this:

var frm = forms[application.getActiveWindow().controller.getName()];

that frm is just a generic basic form that only has the default servoy properties (not even data providers)
That is because servoy doesn’t know which form you talk to. So it doesn’t know that you can fall frm.deleteRecord() (i guess that is a function you added to a specific form?)

If you want to target a specific form (that can be a base form used by many other forms) then you have to cast:

/** @type {RuntimeForm */
var frm = forms[application.getActiveWindow().controller.getName()];

FormName is then the form name of the one that does have those methods.

Johan,

Thank you very much for your response.

I have a generic form with CRUD buttons. The methods in this should do is capture the name of the form where the user is at the moment (the active form), and then trigger the methods (C, R, U or D functions) in the active form.

Using /** @type {RuntimeForm */ won’t work -as I see it.

See the image below. With it I try to explain what I am trying to do (as I always did before Servoy 7).

[attachment=0]Capture.JPG[/attachment]

The form ui_crud_buttons is my generic form with the CRUD buttons.

#1 is the function of the delete button. (deleteRecordBaseForm).

#2 when the user click on a delete button, the function is supposed to capture the name of the active form with:
var frm = forms[application.getActiveWindow().controller.getName()];

#3 then the function should trigger the delete function (and the show all function) of the active form.
frm.deleteRecord(event);
frm.showAllRecords(event);

How can I do this in Servoy 7. 3.1 without getting the warnings?

Thanks,
JC

Hi Juan-Carlos,

Let me make 2 points.

  1. A warning marker is NOT an error or might NOT even result in wrong behavior. It’s just that the code parser in Developer thinks there is something possibly wrong because it can’t understand it. Although it can be VERY helpful in finding issues I have seen it throw warning markers on things that are completely valid and work fine when run.
  2. the frm variable holds a RuntimeForm reference. But you then reference 2 methods on that form that are non standard and/or is not explained to the Developer code parser which RuntimeForm this is. And of course you can’t tell it which one it is at design time because this is a completely dynamic method.
    So you have 2 options.
  • you can ignore this warning marker.
  • rewrite your code so that it doesn’t show this warning marker (but also can make your coding life potentially harder)

Keep in mind the warning markers are there to facilitate the programmer to find programming/logical errors it overlooked. In my experience they are not always correct.
If you are annoyed by these warning markers just remember you can disable them in the preferences. Problem solved.

Hope this helps.

Edit: Or what he (pointing to next post) says…

How do you know that that the active form has those methods??
They just have them?
Are those “deleteRecord” not nicely in a super form so that you know for sure that those forms that you show as active (that the CRUD targets) really has those methods…

if you want to get rid of the warnings then, i say again, you need to cast it:

/** @type {RuntimeForm<BasicFormThatHasThoseDeleteMethodsName>} */
var frm = forms[application.getActiveWindow().controller.getName());
frm.deleteRecord(event)

Or if you really want to be completely dynamic because you just assume that the form has those methods

var frm = forms[application.getActiveWindow().controller.getName());
frm['deleteRecord'](event)

but better would be to really test this and log:

var frm = forms[application.getActiveWindow().controller.getName());
if (frm['deleteRecord']) frm['deleteRecord'](event)
else application.output("form: " + application.getActiveWindow().controller.getName() + " does not have the expected method deleteRecord");

ROCLASI:
Let me make 2 points.

  1. A warning marker is NOT an error or might NOT even result in wrong behavior. It’s just that the code parser in Developer thinks there is something possibly wrong because it can’t understand it. Although it can be VERY helpful in finding issues I have seen it throw warning markers on things that are completely valid and work fine when run.
  2. the frm variable holds a RuntimeForm reference. But you then reference 2 methods on that form that are non standard and/or is not explained to the Developer code parser which RuntimeForm this is. And of course you can’t tell it which one it is at design time because this is a completely dynamic method.

Good to know from an expert. Thanks!

ROCLASI:
So you have 2 options.

  • you can ignore this warning marker.
  • rewrite your code so that it doesn’t show this warning marker (but also can make your coding life potentially harder)

I think I’ll take the first option for now. I’ll definitely rewrite the code later on.

Thank you very much Robert!
JC

jcompagner:
How do you know that that the active form has those methods??
They just have them?
Are those “deleteRecord” not nicely in a super form so that you know for sure that those forms that you show as active (that the CRUD targets) really has those methods…

if you want to get rid of the warnings then, i say again, you need to cast it:

/** @type {RuntimeForm<BasicFormThatHasThoseDeleteMethodsName>} */

var frm = forms[application.getActiveWindow().controller.getName());
frm.deleteRecord(event)




Or if you really want to be completely dynamic because you just assume that the form has those methods 



var frm = forms[application.getActiveWindow().controller.getName());
frm’deleteRecord’




but better would be to really test this and log:


var frm = forms[application.getActiveWindow().controller.getName());
if (frm[‘deleteRecord’]) frm’deleteRecord’
else application.output(“form: " + application.getActiveWindow().controller.getName() + " does not have the expected method deleteRecord”);

Yes, the methods exist (I can assume they are there) but as Rob explained above, the frm variable holds a RuntimeForm reference that will only be visible at run-time but not by the Developer code parser. It is completely dynamic.

I’ll definitely use the test above during the development.Thank you!

Johan, I really appreciate your help.

Best, JC