Flexible JavaScript

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=“Branch Info”;
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=“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=‘Branch Info’;
forms.controller_main.elements.b_nav_<<insert_current_form_name>>.bgcolor=“#D3D3D3”;

david:
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=“Branch Info”;
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=“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=‘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=‘’ +
’ ’ +
’ ’ +
‘+currentcontroller.getName()+’

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!

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.

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();

david:
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()

bingo!

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

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?