[extensions-web: 25/30] hashparamutils: Use jQuery.param, introduce a new "setHashParam" method



commit 0217c9b31ab8c547d815edd4fe94870edda57810
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Jan 9 16:20:43 2012 -0500

    hashparamutils: Use jQuery.param, introduce a new "setHashParam" method

 sweettooth/static/js/fsui.js           |    4 +---
 sweettooth/static/js/hashparamutils.js |   26 ++++++++++++++------------
 sweettooth/static/js/paginator.js      |    2 +-
 3 files changed, 16 insertions(+), 16 deletions(-)
---
diff --git a/sweettooth/static/js/fsui.js b/sweettooth/static/js/fsui.js
index 0178cb4..3908746 100644
--- a/sweettooth/static/js/fsui.js
+++ b/sweettooth/static/js/fsui.js
@@ -9,9 +9,7 @@ require(['jquery', 'hashparamutils', 'modal'], function($, hashparamutils, modal
         return $('<li>', {'class': 'fsui-selection-link'}).
             text(text).
             click(function() {
-                var hp = hashparamutils.getHashParams();
-                hp[name] = value;
-                hashparamutils.setHashParams(hp);
+                hashparamutils.setHashParam(name, value);
             });
     }
 
diff --git a/sweettooth/static/js/hashparamutils.js b/sweettooth/static/js/hashparamutils.js
index dee9d8c..5c8e2f5 100644
--- a/sweettooth/static/js/hashparamutils.js
+++ b/sweettooth/static/js/hashparamutils.js
@@ -1,6 +1,6 @@
 "use strict";
 
-define([], function() {
+define(["jquery"], function($) {
 
     function getHashParams() {
         var hash = window.location.hash;
@@ -14,28 +14,30 @@ define([], function() {
                 continue;
 
             var kv = values[i].split('=');
-            obj[kv[0]] = kv[1];
+            var key = kv[0], value = kv[1];
+            if (key in obj && $.isArray(obj[key]))
+                obj[key].push(value);
+            else if (key in obj)
+                obj[key] = [obj[key], value];
+            else
+                obj[key] = value;
         }
 
         return obj;
     }
 
-    function makeHashParams(obj) {
-        var hash = '';
-        for (var key in obj) {
-            hash += key + '=' + obj[key] + '&';
-        }
-
-        // Remove last '&'
-        return hash.slice(0, -1);
+    function setHashParam(name, value) {
+        var hp = getHashParams();
+        hp[name] = value;
+        setHashParams(hp);
     }
 
     function setHashParams(obj) {
-        window.location.hash = makeHashParams(obj);
+        window.location.hash = $.param(obj);
     }
 
     return { getHashParams: getHashParams,
-             makeHashParams: makeHashParams,
+             setHashParam:  setHashParam,
              setHashParams: setHashParams };
 
 });
diff --git a/sweettooth/static/js/paginator.js b/sweettooth/static/js/paginator.js
index 01fb7fa..cafddd8 100644
--- a/sweettooth/static/js/paginator.js
+++ b/sweettooth/static/js/paginator.js
@@ -61,7 +61,7 @@ define(['jquery', 'hashparamutils', 'jquery.hashchange'], function($, hashparamu
             hp.page = pageNumber;
 
             return $('<a>', {'class': 'number ' + styleClass,
-                             'href': '#' + hashparamutils.makeHashParams(hp)}).text(text);
+                             'href': '#' + $.param(hp)}).text(text);
         }
 
         function buildPaginator() {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]