Backbone - how to maintain state in my case

76 views Asked by At

I've read a lot of threads here but can't find a real answer.

I'm building a desktop app that first loads a lot of json records (let's call them "cards"). Then the user can filter them down with by using many checkboxes, so he can "sum/substract" options (read: query vars).

I'm also trying to use routes.

So, I can have for example (and I don't really know if I'm doing it the right way):

  • app.com/#/cards/?near_address=London
  • app.com/#/cards/?near_address=London&cat=concerts
  • app.com/#/cards/?near_address=London&cat=concerts&day=8
  • app.com/#/cards/?near_address=London&radius=10000 [...]

Basically, every time the user change filters, I should add/remove query vars.

I could do it in many ways, for examble using some simple "state" json object, but I'm not sure it would be a good practice, mostly because I'm not really sure if I can instead do it maybe only by using routes (I'm quite new to the routes concept).

Is there any "good practice" in doing this kind of things with backbone?

thank you

1

There are 1 answers

0
kay.one On

Using routes to store state is actually a very good idea. That's what routes are there for, to store state, but it's up to you how granular you would want to go with them. some developers choose to go route per page and some choose to get more granular like your example and that's perfectly fine too. You just have to be careful not to go overboard and make your URLs too long and cryptic.

Using routes to store states gives your a few really important benefits:

  • Your pages will be refreshable. There is nothing more annoying than refreshing a page and losing all your progress and be take back to the start page of the app.

  • Your URIs are sharable. I could create a filter see the result and send the uri to anyone and they would see the same page as I was.

  • They make your life easier as a dev. which goes back to your pages being refreshable, they allow you to change your styles or scripts files and refresh the page and see the updated page without having to navigate through your app to get back to the same view over and over again.