[Proto-Scripty] RE: returning a value from a function calling a page with Ajax

2010-06-03 Thread Scott
In case this replay didn't end up in the appropriate thread:

http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/
db829a1d3ca58d79

 

REPLY:

 

So here is my simple test using your method:

 

I had to change the 'onSuccess' method in the updater to 'onComplete'

it wasn't working for some reason. I don't remember if 'onSuccess' is

after the DOM is loaded or not.

 



   

   Simple callback test

   

   

   function userChooser(callback) {

   $('readout').insert({ bottom: 'loading callback.vb.asp...
' }); new Ajax.Updater('divID', 'callback.vb.asp', { onComplete: function() { updateSuccess(); $('readout').insert({ bottom: 'loading success!
' }); } }); function updateSuccess() { $('divID').down("input[name=btnChoose]").observe('click', chosenClick); $('readout').insert({ bottom: 'observing button...
' }); } function chosenClick(event) { var theDiv; event.stop(); $('readout').insert({ bottom: 'event stopped...
' }); theDiv = $('divID'); theDiv.stopObserving('click', chosenClick); $('readout').insert({ bottom: 'observing stopped...
' }); callback(theDiv.down('select[name=choices]').getValue()); } } Event.observe(window, 'load', function() { userChooser(choiceMade); function choiceMade(choice) { $('readout').insert({ bottom: choice + '
' }); } }); body{margin:0px;padding:0px;} #divID{border:solid 1px #000;width:400px;} #readout{font:normal 8pt Arial;width:400px;} and callback.vb.asp: Bob Dave Joe Mick It works perfectly. As long as I disable any other user input until they have chosen a user, this should work out great. Again sorry for the long wait for my response. Your work is much appreciated! Thank you! -- 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.

[Proto-Scripty] Re: returning a value from a function calling a page with Ajax

2010-03-29 Thread T.J. Crowder
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  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.