0

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

4

1 回答 1

1

使用路由存储状态实际上是一个非常好的主意。这就是路由的用途,用于存储状态,但这取决于您希望使用它们的粒度。一些开发人员选择按页面进行路由,而另一些开发人员选择像您的示例一样变得更细化,这也很好。你只需要小心不要过度,让你的 URL 太长和神秘。

使用路由来存储状态会给你一些非常重要的好处:

  • 您的页面将是可刷新的。没有什么比刷新页面并丢失所有进度并返回到应用程序的起始页面更烦人的了。

  • 您的 URI 是可共享的。我可以创建一个过滤器查看结果并将 uri 发送给任何人,他们会看到与我相同的页面。

  • 它们使您作为开发人员的生活更轻松。这可以追溯到您的页面可刷新,它们允许您更改样式或脚本文件并刷新页面并查看更新的页面,而无需浏览您的应用程序以一遍又一遍地返回相同的视图。

于 2014-01-28T18:04:57.437 回答