navigate route with querystring

You need to add another route with that expecting parameter : routes: { ‘posts?foo=:foo’ : ‘showPosts’, ‘posts’: ‘showPosts’ }, showPosts: function (foo) { if(typeof foo != ‘undefined’){ // foo parameters was passed } test = true; } update You could define the general route to return all the query string and then parse it in … Read more

backbone.js use different urls for model save and fetch

If you’re reading the source you probably already have a working solution. You essentially have two options (probably more)- Pass URL in save()/fetch() save() takes two parameters attr and options attr – is a hash of model attributes and is used to update the model before save. eg. myModel.save(attrs) is equivalent to myModel.set(attrs) myModel.save() The … Read more

backbone.js set model array property

the syntax you’re trying doesn’t work because the parameters sent into the set method are an object literal. the values on the left side of the : are treated as literal names, while the values on the right can be executed / interpreted code. there’s a few things you can do, though: get, update, and … Read more

Proper way to sort a backbone.js collection on the fly

Interesting question. I would try a variant on the strategy pattern here. You could create a hash of sorting functions, then set comparator based on the selected member of the hash: App.SomeCollection = Backbone.Collection.extend({ comparator: strategies[selectedStrategy], strategies: { firstName: function () { /* first name sorting implementation here */ }, lastName: function () { /* … Read more

Filter backbone collection by attribute value

I like returning a new instance of the collection. This makes these filtering methods chainable (boxes.byColor(“red”).bySize(“L”), for example). var Boxes = Backbone.Collection.extend({ model: Box, byColor: function (color) { filtered = this.filter(function (box) { return box.get(“color”) === color; }); return new Boxes(filtered); } }); var red_boxes = boxes.byColor(“red”)