newButton method

I am new to Servoy and experimenting with the newButton method. In the onLoad trigger, I have the following code:

function onLoad(event) {
var form = solutionModel.newForm(‘newForm1’, null, null, true, 800, 600);
var doNothingMethod = form.newMethod(‘function doNothing() { application.output(“Doing nothing.”); }’);
var onClickMethod = form.newMethod(‘function onClick(event) { application.output("I was clicked at " + event.getTimestamp()); }’);
var onDoubleClickMethod = form.newMethod(‘function onDoubleClick(event) { application.output("I was double-clicked at " + event.getTimestamp()); }’);
var onRightClickMethod = form.newMethod(‘function onRightClick(event) { application.output("I was right-clicked at " + event.getTimestamp()); }’);
var btn = form.newButton(‘I am a button’, 10, 40, 200, 20, doNothingMethod);
btn.onAction = onClickMethod;
btn.onDoubleClick = onDoubleClickMethod;
btn.onRightClick = onRightClickMethod;
controller.recreateUI();
}

The existing form creates a new form called newForm1 and everything works as expected. However, when I try adding a button to an existing form with the following code:
function onLoad(event) {
var form = solutionModel.getForm(controller.getName());
var doNothingMethod = form.newMethod(‘function doNothing() { application.output(“Doing nothing.”); }’);
var onClickMethod = form.newMethod(‘function onClick(event) { application.output("I was clicked at " + event.getTimestamp()); }’);
var onDoubleClickMethod = form.newMethod(‘function onDoubleClick(event) { application.output("I was double-clicked at " + event.getTimestamp()); }’);
var onRightClickMethod = form.newMethod(‘function onRightClick(event) { application.output("I was right-clicked at " + event.getTimestamp()); }’);
var btn = form.newButton(‘I am a button’, 10, 40, 200, 20, doNothingMethod);
btn.onAction = onClickMethod;
btn.onDoubleClick = onDoubleClickMethod;
btn.onRightClick = onRightClickMethod;
controller.recreateUI();
}

The button gets created all right but the onAction, onDoubleClick and onRightClick methods do not get fired.

Is this a Servoy bug or have I missed something?

Hi,

Try inserting the following code after the newMethod calls and before the newButton method. I think the problem is that the methods don’t exist yet when the button(s) are added.

	currentcontroller.recreateUI();
	servoyDeveloper.save();

The currentcontroller.recreateUI() call at the end of your code should also stay.

Cheers,

From 6.1.3 on it is supported to create variables and methods that are the usable after a controller.recreateUI() call.
Before that you have to destroy the form (remove from history) so that it is really recreated from scratch (not just the ui)
the method recreateUI does tell that already it creates the ui, not the data/scripting part. But we did enhance this a bit

“servoyDeveloper.save();”

that call is only for developer specific stuff, where you want to save forms in a client really back into the developers/eclipse workspace, its very specific and won’t work in a normal client.

Hi Johan,

Then it looks like there is a problem when running from Servoy Developer because in my testform without the servoyDeveloper.save() I get the same behaviour as ylockson (the action event does not get fired).

Greetz,
Omar

Thanks Johan & Omar for your responses. As suggested, I have inserted:
servoyDeveloper.save();
Now the actions get fired correctly. But, I wonder whether it would still work in deployment/runtime mode (in both smart client & web)…

To try to get rid of the warning message saying that the form is stale and in view to remove the form from history, I have inserted at the very beginning of the “onLoad” method the following:
history.removeForm(controller.getName());

Unfortunately the ‘stale form’ warning message does not go away (when the form is activated for the first time). What did I do wrong?

Finally, I must confess that I also don’t quite understand the purpose of the “servoyDeveloper” object… Is it designed for use in form generation?

Will Servoy version 6.1.3 be released in the very near future?

This is what my “general purpose” mehtod looks like. This works correctly (in Developer anyway). Note that I used currentcontroller.recreateUI() twice.

function addButton() {
	// var method = form.newFormMethod('function onAction(event) { application.output("onAction intercepted on " + event.getFormName()); }');
	// var button = form.newButton('myButton', 10, 10, 100, 30, method);
	var componentPos = nextComponentPos();
	var buttonName = nextButtonName();
	var methodName = "on" + utils.stringInitCap(buttonName)+"_Action";	
	var method = globals.__ActiveForm.newMethod('function '+methodName+'(event) {\n\t plugins.dialogs.showInfoDialog("Test","'+methodName+' event intercepted");\n}');
	currentcontroller.recreateUI();
	servoyDeveloper.save();
	var button = globals.__ActiveForm.newButton('Button',componentPos[0],componentPos[1],80,20,method);
	button.name = buttonName;
	currentcontroller.recreateUI();
	setActiveComponent(button);
}

Btw if stuff works in Servoy developer but not at runtime then we really have a problem.

as i said, servoyDeveloper.save() is really special, only works in the developer won’t work in any kind of other client only mode.

What developer save does it saving really to the workspace (so your changes are permanent!!) its the same if you would have add the method in the developer itself through the script editor.

So don’t use servoyDeveloper.save, only use it if you really know what you are doing and what you want (and that is saving the clients form changes back to your developer workspace) when running the client in developer.

Again, currently it is NOT support that you create new methods on the fly of existing form instance that are live and kicking.
You have to destroy the form, you can do that by the call history.remove(formname). Do check the return type it should return true it can return false if the form is the visible form.

Support for adding method on the fly on existing form instances is only supported in the next release (6.1.3)

Ok Johan, Thanks for clarifying that. Looking out for 6.1.3 :D

Once again, thanks Johan & Omar for your prompt enlightening responses. It is well appreciated…

Awaiting release 6.1.3 with anticipation too.