Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/177004

Change subject: Add jquery.serializeObject
......................................................................

Add jquery.serializeObject

Teeny-tiny jQuery plugin to get form data as an object mapping each form
control's name to its value. I needed this for something and I saw this
pattern already in use in mediawiki.searchSuggest.js, so that's two use-cases.
I thought about calling this 'jquery.formData', but since it's a close analog of
jquery.serializeArray, I think this name is appropriate.

Change-Id: I00b6a7fb75a4d5132e7d6715c8867091a161dbb6
---
M resources/Resources.php
A resources/src/jquery/jquery.serializeObject.js
M resources/src/mediawiki/mediawiki.searchSuggest.js
3 files changed, 26 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/04/177004/1

diff --git a/resources/Resources.php b/resources/Resources.php
index 88965d0..840ab2c 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -288,6 +288,9 @@
                'scripts' => 'resources/lib/jquery/jquery.jStorage.js',
                'dependencies' => 'json',
        ),
+       'jquery.serializeObject' => array(
+               'scripts' => 'resources/src/jquery/jquery.serializeObject.js',
+       ),
        'jquery.suggestions' => array(
                'scripts' => 'resources/src/jquery/jquery.suggestions.js',
                'styles' => 'resources/src/jquery/jquery.suggestions.css',
@@ -949,6 +952,7 @@
                        'jquery.client',
                        'jquery.placeholder',
                        'jquery.suggestions',
+                       'jquery.serializeObject',
                        'mediawiki.api',
                ),
        ),
diff --git a/resources/src/jquery/jquery.serializeObject.js 
b/resources/src/jquery/jquery.serializeObject.js
new file mode 100644
index 0000000..be046d2
--- /dev/null
+++ b/resources/src/jquery/jquery.serializeObject.js
@@ -0,0 +1,21 @@
+/**
+ * @class jQuery.plugin.serializeObject
+ */
+
+/**
+ * Get form data as a plain object mapping form control names to their values.
+ *
+ * @return {Object}
+ */
+jQuery.fn.serializeObject = function () {
+       var fields = this.serializeArray(), data = {}, field;
+       while ( field = fields.pop() ) {
+               data[field.name] = field.value;
+       }
+       return data;
+};
+
+/**
+ * @class jQuery
+ * @mixins jQuery.plugin.serializeObject
+ */
diff --git a/resources/src/mediawiki/mediawiki.searchSuggest.js 
b/resources/src/mediawiki/mediawiki.searchSuggest.js
index a214cb3..d372e8f 100644
--- a/resources/src/mediawiki/mediawiki.searchSuggest.js
+++ b/resources/src/mediawiki/mediawiki.searchSuggest.js
@@ -41,10 +41,7 @@
                        baseHref = $form.attr( 'action' );
                        baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?';
 
-                       linkParams = {};
-                       $.each( $form.serializeArray(), function ( idx, obj ) {
-                               linkParams[ obj.name ] = obj.value;
-                       } );
+                       linkParams = $form.serializeObject();
 
                        return {
                                textParam: context.data.$textbox.attr( 'name' ),

-- 
To view, visit https://gerrit.wikimedia.org/r/177004
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I00b6a7fb75a4d5132e7d6715c8867091a161dbb6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to