Created
May 23, 2014 02:29
-
-
Save dburles/033b9884db88700390d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Router.query = (function(router) { | |
var current = function() { | |
return router.current(); | |
}; | |
var currentParams = function() { | |
return current().params; | |
}; | |
var currentQueryParams = function() { | |
return current().queryParams; | |
}; | |
var get = function(name) { | |
var values = currentQueryParams()[name]; | |
if (! values) | |
return []; | |
if (! _.isArray(values)) | |
values = [values]; | |
return values; | |
}; | |
return { | |
set: function(name, value) { | |
var query = EJSON.clone(currentQueryParams()); | |
if (! value || ! value.length) | |
delete query[name]; | |
else | |
query[name] = value; | |
router.go(current().route.name, currentParams(), { query: query }); | |
}, | |
add: function(name, value) { | |
var values = get(name); | |
if (_.indexOf(values, value) === -1) | |
values.push(value); | |
this.set(name, values); | |
}, | |
remove: function(name, value) { | |
this.set(name, _.without(get(name), value)); | |
} | |
}; | |
})(Router); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment