Page 1 of 1

varying method behaviour depending on how form was invoked?

PostPosted: Mon Feb 21, 2005 9:14 am
by Neale
I have a, er, "challenge" which involves 3 tables, each with a corresponding form. Let's call these tables "a", "b" and "c" where c is a child of both a and b. Each table has a form: forma, formb and formc.

To state it generally, from formc I want to be able to run a method (e.g. from a button on formc) which behaves slightly differently depending on if formc was reached via the relation tablea_to_tablec or via tableb_to_tablec.

More particularly, formc is to show a listing of the set of table-c records which are children of either a particular table-a record or of a particular table-b record. formc has a button to "add record". If formc was reached via the relation tablea_to_tablec then the "add" method should construct a child record by assuming the current table-a parent and prompting the user for a
record from table-b to use as the other parent (and vice-versa).

Currently I can see only two ways of handling this:

1) replicate formc - call formc-a from forma and formc-b from formb :-(

2) on displaying formc, set the name of the button element to indicate via which relation formc was reached. Maybe I'm being paranoid, but this smells to me like the dreaded "self-modifying code" (which IIRC we discarded as a Bad Idea over a quarter of a century ago).

Any other suggestions?

Thanks,
Neale.

PostPosted: Mon Feb 21, 2005 11:03 am
by ROCLASI
So if I understand correctly you have this:
Code: Select all
+-------+    +-------+    +-------+
|   a   |---<|   c   |>---|   b   |
+-------+    +-------+    +-------+
  forma        formc        formb

Since you have your add button on formc I would use a global variable to track which form (a/b) is in use. myCurrentContextThis way you can create a new record in context of the form you are in.
Lets call this global myCurrentContext and is of type Text.
Lets say you are in forma. You store in your global the name and relationship name from that form to tablec.
Thus something like 'forma.forma_to_formc'.
And in formb we have a method that sets the global to 'formb.formb_to_formc'.

Now in formc you have your add button.
Here we use the following code:
Code: Select all
forms[globals.myCurrentContext].newRecord();

Also in this way you are ready to use it with more than those 2 forms already :)

Hope this helps.

PostPosted: Tue Feb 22, 2005 6:28 am
by Neale
Yes, that diagram is spot-on.

I ended up solving it slightly differently, partly because I'm already collecting too many Global variables and don't want to pollute the namespace further when possible and partly because there's some extra processing before the newRecord()

Essentially formc is a table-view in a tab-panel on either forma or formb. On formc there is a Title Header part with an"add record" button. Each of forma and formb have an onDisplay method which sets the toolTipText property of the button on formc. The method invoked by the "add user" button then has:
Code: Select all
switch( elements.buttonAdd.toolTipText )
{
case 'foo':
  // code for case foo
  break;
case 'bar':
  // code for case bar
  break;
default:
  // complain
}


Best of all, it appears to work just fine :-)

Thanks,
Neale.