T.J., Thanks for that... my confusion came in trying to declare the onSuccess or onFailure functions with the transport parameter... I had no idea that 'transport' would be passed regardless... once that was cleared up, it seems to be working fine!
On Sun, Aug 8, 2010 at 4:23 AM, T.J. Crowder <[email protected]> wrote: > Hi, > > You seem to be using Ajax.Updater when you should be using > Ajax.Request, but that may be off-topic as it's true for both of your > examples; perhaps I'm misunderstanding what you're doing. Ajax.Updater > does an Ajax request and updates a target element with the response. > But your code is using onSuccess to update _other_ things based on an > XML response, which looks more like the kind of thing you'd use > Ajax.Request for. > > Again, all of which may be off topic. > > In terms of moving from an anonymous function to a named one, what > you've done should work although you're actually working harder than > you have to. Taking a simplified example, this version using an inline > anonymous function: > > new Ajax.Request('someurl', { > parameters: {"what": "ever"}, > onSuccess: function(response) { > // Do something with 'response' > } > }); > > ...is functionally equivalent to this example using a named function > (with only one small difference, a new symbol -- the function's name > -- is defined): > > function handleSuccess(response) { > // Do something with 'response' > } > > new Ajax.Request('someurl', { > parameters: {"what": "ever"}, > onSuccess: handleSuccess > }); > > Note how that differs from what you did; the above just uses a > reference to the function directly. What you did: > > new Ajax.Request('someurl', { > parameters: {"what": "ever"}, > onSuccess: function(response) { > handleSuccess(response); > } > }); > > ...should work just fine, but introduces an unnecessary function > definition and call. > > Since what you did, from the quoted code, should have worked, not sure > how much the above is going to help. Here's a live example of doing > requests with a separate function for the success handler, perhaps > it'll help: > http://jsbin.com/uwofi4 > > This how-to on the unofficial wiki may be useful as well: > http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests > > HTH, > -- > T.J. Crowder > Independent Software Consultant > tj / crowder software / com > www.crowdersoftware.com > > > On Aug 6, 8:44 pm, ppetree <[email protected]> wrote: > > I've started with the samples included with prototype.js which has the > > following code in the fillin example (a portion of which I want to > > isolate into its own function). > > > > The original code looks like this ([COLOR="red"]red[/COLOR] is what I > > want to isolate):[CODE] > > function dofill( ) { > > new Ajax.Updater( 'result', 'getdata.php', { method: 'post', > > parameters: $('myform').serialize(), > > onSuccess: [COLOR="Red"]function( transport ) { > > $('elFirst').value = > > transport.responseXML.getElementsByTagName('first') > > [0].firstChild.nodeValue; > > $('elLast').value = > > transport.responseXML.getElementsByTagName('last') > > [0].firstChild.nodeValue; > > $('elEmail').value = > > transport.responseXML.getElementsByTagName('email') > > [0].firstChild.nodeValue; > > }[/COLOR] } );} > > > > [/CODE] > > > > What I created was this:[CODE] > > function fill_in(transport) { > > $('elFirst').value = > > transport.responseXML.getElementsByTagName('first') > > [0].firstChild.nodeValue; > > $('elLast').value = > > transport.responseXML.getElementsByTagName('last') > > [0].firstChild.nodeValue; > > $('elEmail').value = > > transport.responseXML.getElementsByTagName('email') > > [0].firstChild.nodeValue;} > > > > function dofill() { > > new Ajax.Updater( 'result', 'getdata.php', { method: 'post', > > parameters: $('myform').serialize(), > > onSuccess: function ( transport ){ > > fill_in(transport); > > } } );} > > > > [/CODE] > > > > I cant seem to get this to work... but I'm not getting any errors > > either. The idea is to make the fill_in() independent so that it can > > be easily changed and updated and this allows the dofill() to be stuck > > in a .js file. > > > > I dont understand why transport wont just pass as a variable. How do > > I do this? > > > > Thanks, > > > > Pete > > -- > 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 > [email protected]. > To unsubscribe from this group, send email to > [email protected]<prototype-scriptaculous%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/prototype-scriptaculous?hl=en. > > -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
