From 343eae8bfc88bfafbeb4d4d15f7736def5b3dd9d Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 28 Jan 2007 07:30:04 +0000 Subject: [PATCH] prototype: Add Form.Methods.request as a convenience method for serializing and submitting a form via Ajax.Request to the URL in the form's action attribute. --- CHANGELOG | 6 ++++++ src/ajax.js | 22 ++++++++++++---------- src/form.js | 17 +++++++++++++++++ test/unit/fixtures/empty.js | 1 + test/unit/form.html | 27 +++++++++++++++++++++++++-- 5 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 test/unit/fixtures/empty.js diff --git a/CHANGELOG b/CHANGELOG index 12607d7..d4f27c2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* Add Form.Methods.request as a convenience method for serializing and submitting a form via Ajax.Request to the URL in the form's action attribute. [sam] + + Options passed to request() are intelligently merged with the underlying Ajax.Request options: + - If the form has a method attribute, its value is used for the Ajax.Request method option. If a method option is passed to request(), it takes precedence over the form's method attribute. If neither is specified, method defaults to "post". + - Key/value pairs specified in the parameters option (either as a query string or as a hash) will be merged with (and take precedence over) the serialized form parameters. + * Fix $(form).serialize() in Safari and add support for extending specific tags to Element.addMethods. Closes #7358. [Andrew Dupont] * Add String.prototype.startsWith, String.prototype.endsWith, and String.prototype.include. Closes #7075. [Tobie Langel] diff --git a/src/ajax.js b/src/ajax.js index 518dedb..a661ee9 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -82,7 +82,7 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), { request: function(url) { this.url = url; this.method = this.options.method; - var params = this.options.parameters; + var params = Object.clone(this.options.parameters); if (!['get', 'post'].include(this.method)) { // simulate other verbs over post @@ -90,12 +90,15 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), { this.method = 'post'; } - params = Hash.toQueryString(params); - if (params && /Konqueror|Safari|KHTML/.test(navigator.userAgent)) params += '&_=' - - // when GET, append parameters to URL - if (this.method == 'get' && params) - this.url += (this.url.include('?') ? '&' : '?') + params; + this.parameters = params; + + if (params = Hash.toQueryString(params)) { + // when GET, append parameters to URL + if (this.method == 'get' && params) + this.url += (this.url.include('?') ? '&' : '?') + params; + else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) + params += '&_='; + } try { Ajax.Responders.dispatch('onCreate', this, this.transport); @@ -109,9 +112,8 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), { this.transport.onreadystatechange = this.onStateChange.bind(this); this.setRequestHeaders(); - var body = this.method == 'post' ? (this.options.postBody || params) : null; - - this.transport.send(body); + this.body = this.method == 'post' ? (this.options.postBody || params) : null; + this.transport.send(this.body); /* Force Firefox to handle ready state 4 for synchronous requests */ if (!this.options.asynchronous && this.transport.overrideMimeType) diff --git a/src/form.js b/src/form.js index 233d160..13b7929 100644 --- a/src/form.js +++ b/src/form.js @@ -82,6 +82,23 @@ Form.Methods = { form = $(form); form.findFirstElement().activate(); return form; + }, + + request: function(form, options) { + form = $(form), options = Object.clone(options || {}); + + var params = options.parameters; + options.parameters = form.serialize(true); + + if (params) { + if (typeof params == 'string') params = params.toQueryParams(); + Object.extend(options.parameters, params); + } + + if (form.hasAttribute('method') && !options.method) + options.method = form.method; + + return new Ajax.Request(form.action, options); } } diff --git a/test/unit/fixtures/empty.js b/test/unit/fixtures/empty.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/unit/fixtures/empty.js @@ -0,0 +1 @@ + diff --git a/test/unit/form.html b/test/unit/form.html index 363135e..106321a 100644 --- a/test/unit/form.html +++ b/test/unit/form.html @@ -23,14 +23,14 @@
-
+
This is not a form element
-
+