Hi,

You can't make it wait, no. The way to do this is to have your
`userChooser` function accept a callback function reference that the
user choosing code will call when the user makes a choice. The
`userChooser` function would then return immediately, but later the
event of the user making a choice would trigger the callback function.

So for instance;

    function userChooser(callback) {
        new Ajax.Updater('divID', 'userChooser.asp', {
            onSuccess: updateSuccess
        });

        function updateSuccess() {
            $('divID').down("input[name=btnChoose]").observe('click',
chosenClick);
        }

        function chosenClick(event) {
            var theDiv;

            event.stop();
            theDiv = $('divID');
            theDiv.stopObserving('click', chosenClick);
            callback(theDiv.down('select[name=choices]').getValue());
        }
    }

And using it:

    function foo() {

        userChooser(choiceMade);

        function choiceMade(choice) {
            /* ...do something with the 'choice' value...*/
        }
    }

(Note that `foo` returns immediately, *before* the choice is made.)

There, we're calling `userChooser` and passing in `choiceMade` as the
`callback` parameter. The Ajax.Updater does its thing and, if
successful, hooks up a handler on the `btnChoose` input element. When
that's clicked, it calls `chosenClick`, which calls the callback with
the value of the `choices` select element. (You'd probably remove the
UI related to choosing in the `chosenClick` function.) There's
obviously a bit of error handling I've left out of the above.

It's a real shift in the way you think about user (and other)
interaction, but once you get the hang of it, it's really powerful.
You basically think in terms of states the application may be in, and
transitions between those states. You have the state before you ask
the question, the state of asking the question, and the state of
having gotten your answer.

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Mar 29, 7:37 pm, Scott <counterstre...@gmail.com> wrote:
> I am trying to build a function that will return a value based on user
> input on a page loaded into a div using Ajax.Updater.
>
> So for example:
>
> function userChooser() {
>   new Ajax.Updater('divID', 'userChooser.asp');
>
> }
>
> alert(userChooser());
>
> Inside userChooser.asp I would display a list that the user can click
> on. When they click on an item in the list, I want the function to
> return that item. What i don't know is if I can make the function wait
> to return until the user clicks on an item (like an alert or confirm
> dialog would do). Also if there is a way to make it wait, I don't know
> how to assign a value back to that function.
>
> Is this possible?

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to