I am getting some exception when I am going to add a new button to a form created by using Solution Model. Here I am describing the ways & the Exception, I am getting.
Adding a button using Solution Model
Way - 1 Code
var _btnObj = _frmObj.newButton("button1",510,26,22,22,
new function() {
plugins.dialogs.showInfoDialog("","Testing...","Ok");
} );
Exception
The code for the function is getting executed first & throwing the below exception.
Cannot convert [object Object] to org.mozilla.javascript.Function
Way - 2 Code
var foo = function() { plugins.dialogs.showInfoDialog("","Testing...","Ok"); };
var _btnObj = _frmObj.newButton("button1",510,26,22,22, foo);
Exception
Here, only throws the below exception.
org.mozilla.javascript.UniqueTag cannot be cast to java.lang.String
But, if I pass a already crated function to the method, then It is working Okay. But, I would like to create the method at runtime. Am I doing any wrong @ here??
Your first example doesn’t work because you are adding a “new” keyword before “function” in the arguments. “function” (lower case) is a literal and won’t work with “new.”
agiletortoise:
Your first example doesn’t work because you are adding a “new” keyword before “function” in the arguments. “function” (lower case) is a literal and won’t work with “new.”
greg.
Thanks Greg for your reply.
If I will not add the “new” keyword, it is giving the Error
org.mozilla.javascript.UniqueTag cannot be cast to java.lang.String
The final parameter of the newButton function is a ‘Servoy function’, also known as a method. So assume that you had created a global method called __pinsky_foo with the contents of your foo function. The call would become:
var _btnObj = _frmObj.newButton("button1",510,26,22,22,globals.__pinsky_foo);
It seems to me that in order for us to be able to assign our own non-Servoy functions to events, there would need to be the option to create a newMethod under the SolutionModel node.
You are creating the function correctly like this:
var foo = function() { plugins.dialogs.showInfoDialog("","Testing...","Ok"); };
```You just can't attach a function that lives inside the context of your method to a new form that you have created.
troy:
You just can’t attach a function that lives inside the context of your method to a new form that you have created.
Thanks Troy.
troy:
It seems to me that in order for us to be able to assign our own non-Servoy functions to events, there would need to be the option to create a newMethod under the SolutionModel node.
It will be really better to have an option to created a new Servoy Method under the Solution Model (Feature Request). The newly created Servoy Method can be used all through… Won’t it will be better???
we are thinking about that for the next version but the question is why do you want that
Are you going to give your end users a free textfield where they can do some scripting and with that script you make then a new method?
How do those end users know what they can do? Are you going to rebuild our tree (or parts of it) so that they know what the can call?
Thats currently the only thing that i can think of why you want that. Because in the example above you already have the code that you want in the solution object (declared as that function)
so you could declare it as a global just fine.
Well, the solutionModel is being pitched to do anything that you could do in developer at runtime. Form methods are a pretty important aspect of developer that should be available at runtime, it seems to me. If I want to write forms on the fly, every button and action that I want scripted should not have to be linked back to a global method, IMHO, which seems to be the current requirement.
jcompagner:
Are you going to give your end users a free textfield where they can do some scripting and with that script you make then a new method?
Currently, yes. It’s entirely for our convenience though than the end user as it is only a text field.
However, given this functionality we could put together easy-to-use wizards that users could create common task patterns with. That would be pretty cool stuff.
jcompagner:
we are thinking about that for the next version but the question is why do you want that
By using the new Solution Model, we can, now, be able to create almost everything on the fly. Then, why not Servoy Methods? I can create global methods and use them. But, I would like to build everything on the fly.
David has shown a really very good example to give the users(not probably end users) a flexible, dynamic & smart way to handle your application(May be useful for Templet making), Thanks David.
yes david has an example how an end user could add some code
I still think then how does an end user know what it can use? Plugins? Applicaiton? Databasemanager?
So in the end you are rebuilding the complete servoy designer.
If there is no free text entry form where you get code from outside your solution (could maybe also be a database or something) i dont see any usecase currently.
Well, to be honest I see some useful scenarios, you could customize your framework on demand giving the ready to be pasted code to the customer without having to apply the enhancement/modification to all the other framework customers and without forking, forking scares me a lot. Having said that, I don’t know how much demand there could be for this feature. Just my 2 cents.
When the customer asks for a customization you just prepare the code and send it to him, no need to create a global method and change the framework for only one customer.
Another argument for this change is to allow better code organization. Why do the functions need to be declared as globals? I’d much rather (in cases where it makes sense) just define the function in local variable scope rather than cluttering up globals with a bunch of functions that aren’t usable globally anyways. Sorry, I’m new to Servoy so maybe I’m missing other ways of doing this (I’m primarily a Java developer).
I guess I’m also missing why a newMethod() function is required in Solution Model. Why don’t normal Javascript functions just work as parameters to the setOn*Method() functions?