views:

57

answers:

2

Not much knowledge about Javcascript beyond using it for some Dynamic HTML. Now I'm venturing a bit into Ajax ground and have a problem with the following code (borrowed from http://kpumuk.info/php/ajax-enabled-smarty-plugins-part-2-ajax_form/ and edited to fit my needs).

How can I pass the update_id parameter to the obSubmit function?

var SmartyAjax = {

     submit: function(form, update_id, params, callback)
     {
       var myAjax = new Ajax.Request(
       form.action,
       {
         method: form.method,
         parameters: Form.serialize(form.id),
         onComplete: callback || this.onSubmit
       });
     },

     onSubmit: function(originalRequest)
     {
       var results = originalRequest.responseText;
       this.target = $("target2");
       this.target.innerHTML = results;
     }

}

I want to pass update_id to the function onSubmit so that I can assign it as the target. According to the Prototype documentation it gets the Ajax.Response object passed as the first parameter automatically. So, that's what is referenced by originalRequest. I don't see a way to pass update_id to that function as well. How do I do that?

On a related note: I see this "name: function(){}" syntax the first time. What is that? Is that needed because it creates methods of an object? A pointer to a simple explanation would be appreciated.

Thanks very much!

A: 

You need to call the function yourself from an anonymous wrapper (unless you want to modify the Prototype source, which I'd strongly suggest you avoid), like this:

 submit: function(form, update_id, params, callback)
 {
   var complete = callback || this.onSubmit;
   var myAjax = new Ajax.Request(
   form.action,
   {
     method: form.method,
     parameters: Form.serialize(form.id),
     onComplete: function(req) { complete.call(this, req, update_id); }
   });
 },

Now your handler will have a second argument, the update_id parameter passed into this function, for example you could do this:

onSubmit: function(originalRequest, update_id) {
  alert(update_id);
}
Nick Craver
@bolero - Don't *replace* your `onSubmit` with the above, it's just an example showing that whatever you use for `onSubmit` (whether it's the callback or the base `onSubmit`) will now get a second argument. You could for example add that second parameter on the `onSubmit` function signature and use it inside...but the current code in there still needs to run to update the element.
Nick Craver
@bolero - I also see an extra `x` slipped in there, probably throwing an error: `var complete = callback || this.onSubmitx;` should be `var complete = callback || this.onSubmit;`
Nick Craver
I don't understand how I can post comments in the same way that I posted the original question? e.g. with decent formatting of the code. How do I do that?When I carry over your code to my code it runs bit the variable isn't passed somehow. I would like to post the code, but the comnment field just submits unformatted garbage, so I removed my comments.
bolero
@bolero - You can use backticks (tilde key) for *some* formatting, but nothing more than what you see in my last comment.
Nick Craver
The function called is onSubmitx. I just copied the original function and renamed it so I can keep the original in there.
bolero
@bolero - did you change the signature of that method to accept a second parameter?
Nick Craver
Hm, no way. I'm gonna post line by line.
bolero
Yes, I changed the signature. I play a bit with my test comment and then try to post the code.
bolero
Note, in the following code I deliberately removed update_id from the original function signature and assign it directly to make sure there isn't something wrong in the original calling code.
bolero
submit: function(form, params, callback)
bolero
{ var complete = callback || this.onSubmitx;
bolero
var update_id = 'target2';
bolero
var myAjax = new Ajax.Request( form.action, { method: form.method, parameters: Form.serialize(form.id),
bolero
onComplete: function(req) { complete.call(this, req, update_id); } });
bolero
},now the onSubmitx function:
bolero
onSubmitx: function(originalRequest, updated_id) { var results = originalRequest.responseText; this.target = $(update_id); this.target.innerHTML = results; }
bolero
oh, there is 'd' too much.
bolero
@bolero - If you do `alert(update_id)` in that `onSubmitx` function, what do you get?
Nick Craver
Nick, it was a typo, the signature referenced the variable as "updated_id", not "update_id". It's that kind of typo that is hard to detect because the brain identifies whole words rather than single characters. Thanks so much, it's working now! A question about understanding how this works:
bolero
onComplete: function(req) { complete.call(this, req, update_id); }
bolero
@bolero - do you have a specific question, or just overall?
Nick Craver
as I understand I "catch" the Response object and pass it to the next function. Why can't I write that down straight as a function but have to use that call method? (if there is a simple "why" other than "it's the way it is") And what exactly do I pass in that this?
bolero
Still struggling with the interface ;-) That was my question in the last post.
bolero
@bolero - oh you can just do `complete(req, updated_id)`, but you'll lose the context, and `this` will lose it's meaning inside whatever `complete` is. If you don't need `this` then you can just call it directly without `.call()`.
Nick Craver
Ah, well. I think I need 'this'. I'll check that out to see the difference. Thanks for the explanation and the original answer! That took me a whole frustrating afternoon yesterday.
bolero