Flexible JavaScript

Questions, tips and tricks and techniques for scripting in Servoy

Flexible JavaScript

Postby david » Thu May 01, 2003 1:25 am

Say I have the following statements in a method which control the look of a button on a custom controller. The method is called when the form is entered:

forms.controller_main.elements.b_nav_branch_info.text="<html><b>Branch Info</b>";
forms.controller_main.elements.b_nav_branch_info.bgcolor="white";

I have a similar method that is called when the form is exited:

forms.controller_main.elements.b_nav_branch_info.text="<html>Branch Info";
forms.controller_main.elements.b_nav_branch_info.bgcolor="#D3D3D3";

The name of the form is "branch_info". By the time I am done, I could have dozens of forms and I will need both of these methods for each form. Instead of duplicating each of these methods and inserting the correct form name for each form, is it possible to reuse one instance of each method (global method) and substitute the current form name on the fly? Sort of like this:

forms.controller_main.elements.b_nav_<<insert_current_form_name>>.text='<html>Branch Info';
forms.controller_main.elements.b_nav_<<insert_current_form_name>>.bgcolor="#D3D3D3";
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: Flexible JavaScript

Postby maarten » Thu May 01, 2003 9:55 am

david wrote:Say I have the following statements in a method which control the look of a button on a custom controller. The method is called when the form is entered:

forms.controller_main.elements.b_nav_branch_info.text="<html><b>Branch Info</b>";
forms.controller_main.elements.b_nav_branch_info.bgcolor="white";

I have a similar method that is called when the form is exited:

forms.controller_main.elements.b_nav_branch_info.text="<html>Branch Info";
forms.controller_main.elements.b_nav_branch_info.bgcolor="#D3D3D3";

The name of the form is "branch_info". By the time I am done, I could have dozens of forms and I will need both of these methods for each form. Instead of duplicating each of these methods and inserting the correct form name for each form, is it possible to reuse one instance of each method (global method) and substitute the current form name on the fly? Sort of like this:

forms.controller_main.elements.b_nav_<<insert_current_form_name>>.text='<html>Branch Info';
forms.controller_main.elements.b_nav_<<insert_current_form_name>>.bgcolor="#D3D3D3";


Hi David,

1) create a global text "contentButtonX"
2) go into designer, create buttonX.
3) set the property "dataprovider" of buttonX to globals.contentButtonX
4) in the MethodEditor create a global script that goes like this:
------
globals.contentButtonX='<html>' +
' <table width=80>' +
' <td bgcolor = "#D3D3D3" align = "left">'+currentcontroller.getName()+'</td>' +
' </table>'
------
NOTES:
-literal text goes between single quotes
-set the width to the same size as your button. (this way you can use HTML alignment properly
-assign this global script to property "onShow" of your form
(of course you may use the other "event" properties as well)
-you can now attach this script to any button/form in your solution :P
TIPS:
-you may also want to store your html code in separate globals:
globals.HTMLleft + currentcontroller.getName() + globals.HTMLright
-or globals.bgcolor :wink:
(important: globals aren't stored when you close a solution, so best thing to do in this case is create a separate table called settings or main with one record in it, having fields HTMLleft, HTML right, HTMLbgcolor etc..)


last note:
We're testing code right now, using images on buttons.
We already have "rollOverImage"(onMouseOver) that switches images in a button , but we also want to create the possibility to change image with a scriptstep. Hopefully in the next build, but I can't promise right now.

Have fun!
Maarten Berkenbosch
User avatar
maarten
 
Posts: 797
Joined: Wed Apr 23, 2003 10:52 pm
Location: Amersfoort, Netherlands

Postby david » Thu May 01, 2003 9:39 pm

Very helpful response -- I now have navigation buttons on a custom controller that change states depending which form is visible. Doing it this way allows the forward and back history buttons to also change the nav button states (something that the crm solution does not do). In retrospect, I could have just put conditional logic in the forward and back history methods of the crm solution and come up with the same thing.

However, I learned a lot and the implications of using globals and data providers to write 'dynamic' code is starting to sink in. There is a bazillion ways to do a given task -- from grunt to elegant. It will take a while to learn how to do things elegantly.

I must say that for not knowing any JavaScript a couple of days ago I am indeed having fun coding methods. With the editor's easy and helpful organization plus the crm solution examples I'm much farther along than I thought I'd be.
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby david » Fri May 02, 2003 12:33 am

How do I navigate to a form whose name is given in a global? All I can think of is to do a comparison like this:

if (globals.current_form_name == "form_1")
{
forms.form_1.controller.show();
}

Is there a way to go directly to the form without doing a comparison? Again, I'm getting to this idea of wanting to do something like this:

forms.%%globals.current_form_name%%.controller.show();
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby maarten » Fri May 02, 2003 12:43 pm

david wrote:How do I navigate to a form whose name is given in a global? All I can think of is to do a comparison like this:

if (globals.current_form_name == "form_1")
{
forms.form_1.controller.show();
}

Is there a way to go directly to the form without doing a comparison? Again, I'm getting to this idea of wanting to do something like this:

forms.%%globals.current_form_name%%.controller.show();


try this :) >> forms[globals.current_form_name].controller.show()
Maarten Berkenbosch
User avatar
maarten
 
Posts: 797
Joined: Wed Apr 23, 2003 10:52 pm
Location: Amersfoort, Netherlands

Postby david » Fri May 02, 2003 4:28 pm

bingo!
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Referencing Objects Via Globals

Postby bcusick » Sun May 04, 2003 8:36 am

WOW.

LOVE IT!

This will make my life so much easier than creating [if] or [case] statements for everything in the world. :D

THANK YOU DEV TEAM!

Bob
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA

Postby patrick » Thu Sep 04, 2003 2:31 pm

Hello, I have a question regarding flexible Java Script: I want to figure out how many records there are for a given relationship. I have a reference to a dataprovider in the form relation_name.field and am trying to calculate the number of records in relation_name.

This doesn't seem to work (In the example I have to figure out first if there is a relationship required at all, that is most of the code; the question comes at [relation].getMaxRecordIndex();):

var relation_used = field.indexOf('.', 0);
if (relation_used != -1)
{
var relation = field.substr(0, field.indexOf('.', 0));
var num_records = [relation].getMaxRecordIndex();
}


Is there a way to do it or am I doing something wrong?
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany


Return to Methods

Who is online

Users browsing this forum: No registered users and 5 guests

cron