jenkins-bot has submitted this change and it was merged.

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


Add jquery.fn.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
M resources/src/jquery/jquery.getAttrs.js
M resources/src/mediawiki/mediawiki.searchSuggest.js
3 files changed, 21 insertions(+), 13 deletions(-)

Approvals:
  Krinkle: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/Resources.php b/resources/Resources.php
index f2c3227..9755d25 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -951,6 +951,7 @@
                        'jquery.client',
                        'jquery.placeholder',
                        'jquery.suggestions',
+                       'jquery.getAttrs',
                        'mediawiki.api',
                ),
        ),
diff --git a/resources/src/jquery/jquery.getAttrs.js 
b/resources/src/jquery/jquery.getAttrs.js
index 5d6a1d4..a2e2be5 100644
--- a/resources/src/jquery/jquery.getAttrs.js
+++ b/resources/src/jquery/jquery.getAttrs.js
@@ -2,6 +2,16 @@
  * @class jQuery.plugin.getAttrs
  */
 
+function serializeControls( controls ) {
+       var i, data = {}, len = controls.length;
+
+       for ( i = 0; i < len; i++ ) {
+               data[ controls[i].name ] = controls[i].value;
+       }
+
+       return data;
+}
+
 /**
  * Get the attributes of an element directy as a plain object.
  *
@@ -11,16 +21,16 @@
  * @return {Object}
  */
 jQuery.fn.getAttrs = function () {
-       var i,
-               map = this[0].attributes,
-               attrs = {},
-               len = map.length;
+       return serializeControls( this[0].attributes );
+};
 
-       for ( i = 0; i < len; i++ ) {
-               attrs[ map[i].name ] = map[i].value;
-       }
-
-       return attrs;
+/**
+ * Get form data as a plain object mapping form control names to their values.
+ *
+ * @return {Object}
+ */
+jQuery.fn.serializeObject = function () {
+       return serializeControls( this.serializeArray() );
 };
 
 /**
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: merged
Gerrit-Change-Id: I00b6a7fb75a4d5132e7d6715c8867091a161dbb6
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Bartosz DziewoƄski <[email protected]>
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to