Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Facets now have querystrings

  • Loading branch information...
commit bd9a85e04bdabee76079217c5540172aec9f6a42 1 parent bcd782f
authored February 17, 2013
2  src/resources/views/facet.jade
@@ -4,6 +4,6 @@
4 4
     - if (facet.length)
5 5
       each f in facet
6 6
         li
7  
-          a(href="/?#{facetQuery}=#{f._id}")
  7
+          a(href="/#{query.extendQuerystring(facetQuery,f._id)}")
8 8
             span.facet-name= f._id
9 9
             span.facet-amount= f.count
17  src/server/query.coffee
... ...
@@ -0,0 +1,17 @@
  1
+_ = require 'underscore'
  2
+qs = require 'querystring'
  3
+
  4
+exports.createQuery = (previousQuery)->
  5
+
  6
+	extendQuery:(name,value)->
  7
+		if previousQuery.hasOwnProperty name
  8
+			_.omit(previousQuery,name)
  9
+		else
  10
+			q=_.clone(previousQuery)
  11
+			q[name]=value
  12
+			q
  13
+
  14
+
  15
+	extendQuerystring:(name,value)->
  16
+		"?"+qs.stringify(@extendQuery(name,value))
  17
+		
3  src/server/webserver.coffee
... ...
@@ -1,5 +1,6 @@
1 1
 express = require 'express'
2 2
 mongostore = require './mongostore.coffee'
  3
+query = require './query.coffee'
3 4
 domain = require './domain.coffee'
4 5
 
5 6
 mongoPort = process.env.MONGOPORT or 27017
@@ -20,7 +21,7 @@ app.get '/', (req, res)->
20 21
 	search = domain.createSearch mongostore.createMongostore(connectionString)
21 22
 	search.getResults req.query,(err,result)=>
22 23
 		res.send err if err
23  
-		res.render('index',{title:"Your Lego Sets",searchResults:result})
  24
+		res.render('index',{title:"Your Lego Sets",searchResults:result,query:query.createQuery(req.query)})
24 25
 
25 26
 # Start Server
26 27
 app.listen port, -> console.log "LegoDatabase server is listening on #{port}\nPress CTRL-C to stop server."
21  test/server/query_test.coffee
... ...
@@ -0,0 +1,21 @@
  1
+query = require '../../src/server/query.coffee'
  2
+should = require 'should'
  3
+
  4
+describe 'A query', ->
  5
+	it 'should be able to add values to the query',->
  6
+		q=query.createQuery({})
  7
+		q.extendQuery("a","b").should.eql {"a":"b"}
  8
+	it 'should be able to getQuery with multiple values without them being added',->
  9
+		q=query.createQuery({"1":"2"})
  10
+		q.extendQuery("a","b").should.eql {"1":"2","a":"b"}
  11
+		q.extendQuery("c","d").should.eql {"1":"2","c":"d"}
  12
+	it 'should remove the facet from the query if it already exists',->
  13
+		q=query.createQuery({"a":"2"})
  14
+		q.extendQuery("a","3").should.eql {}
  15
+	it 'should be able to output a querystring',->
  16
+		q=query.createQuery({})
  17
+		q.extendQuerystring("a","b").should.equal "?a=b"
  18
+	it 'should be able to output multiple querystring values',->
  19
+		q=query.createQuery({"1":"2"})
  20
+		q.extendQuerystring("a","b").should.equal "?1=2&a=b"
  21
+

0 notes on commit bd9a85e

Please sign in to comment.
Something went wrong with that request. Please try again.