JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://raw.github.com/documentcloud/underscore/1.1.7/underscore.js"></script>
<script src="https://raw.github.com/documentcloud/backbone/0.5.3/backbone.js"></script>
<input class="fetch-button" type="button" value="Fetch" />
JavaScript
var Posts = Backbone.Collection.extend({
url: 'api/posts'
});
// Backbone Router with a custom parameter extractor
var Router = Backbone.Router.extend({
routes: {
'posts/?:filters': 'filterPosts'
},
filterPosts: function(filters){
console.log(filters);
posts.fetch({data: $.param(filters)});
},
_extractParameters: function(route, fragment) {
var result = route.exec(fragment).slice(1);
result.unshift(deparam(result[result.length-1]));
return result.slice(0,-1);
}
});
// simplified $.deparam analog
var deparam = function(paramString){
var result = {};
if( ! paramString){
return result;
}
$.each(paramString.split('&'), function(index, value){
if(value){
var param = value.split('=');
result[param[0]] = param[1];
}
});
return result;
};
var posts = new Posts;
var router = new Router;
Backbone.history.start();
$('.fetch-button').click(function() {
// in your code you should call
// Backbone.history.navigate('posts/?author=martin', true);
// instead of
Backbone.history.loadUrl('posts/?author=martin');
Backbone.history.loadUrl('posts/?price_min=80&price_max=300');
// Backbone.history.navigate will change your address bar string to
// http://myapp.com/s/#posts/?author=martin
// and
// http://myapp.com/s/#posts/?price_min=80&price_max=300
});