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. Accordin开发者_StackOverflowg 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!
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);
}
Can't you just do:
onComplete: callback || this.onSubmit.bind(this, update_id)
精彩评论