1

Backbone define routes in its Controller in the following fashion. Does this mean every page of the site must have a copy of it? Or that every script must be load when the user reach the first page to make it work?

var Workspace = Backbone.Controller.extend({

  routes: {
    "help":                 "help",    // #help
    "search/:query":        "search",  // #search/kiwis
    "search/:query/p:page": "search"   // #search/kiwis/p7
  },
  help: function() {
    ...
  },
  search: function(query, page) {
    ...
  }
});
yves amsellem
  • 7,106
  • 5
  • 44
  • 68

1 Answers1

4

It's a hashbang router, those's aren't real pages. The urls look like:

  • mysite/!#help
  • mysite/!#search/kiwis
  • etc.

It's used to route single page web apps. So you only serve one page and then render other pages by getting your data from a JSON web service.

Backbone.js allows you to route to sub pages on the client inside a page. This means you can change your URL to a book markable state and when you reload the page, backbone will reload that "section" of the page.

This routing should only be used inside a page and should not span across multiple pages.

You should be using your serverside MVC framework for that.

  • CodeIgniter for PHP
  • Express for node.js
  • Rails for Ruby/Groovy
  • MVC for ASP.NET
  • Django for Python
  • etc.
Raynos
  • 166,823
  • 56
  • 351
  • 396
  • So how do we handle multiple pages? – yves amsellem May 31 '11 at 08:49
  • @yvesamsellem you don't use a client side controller across multiple pages. You handle multiple pages using a server side controller. – Raynos May 31 '11 at 09:19
  • Please, can you tell me more on this? Maybe some further reading links? – yves amsellem May 31 '11 at 09:24
  • @yvesamsellem depends completely on what your server-side setup is. I listed some common server-side MVC frameworks. – Raynos May 31 '11 at 09:43
  • I miss something. JavaScript helps us to move everything client-side. Why don't also manage multiple pages client-side? Every other JavaScript solution do not need a server. Why the client needs a server, is this from habits? – yves amsellem May 31 '11 at 09:50
  • @yvesamsellem That's why if you do clientside routing you have only one page. And you do all your logic inside that one page pulling data from a JSON web service. You don't use links to different pages, you just have buttons that change and morph the current page. Besides you need a server to route web pages. I think your getting confused with what you do on the serve and what you do on the client – Raynos May 31 '11 at 10:06
  • @Raynos What prevent us for using `window.location` and managing navigation client-side? You gave me assertions, but I need arguments to understand. Thanks. – yves amsellem May 31 '11 at 10:12
  • @Raynos The role of the server is to serve JSON representation. The role of the client is to access to those representation in JavaScript. I don't see why a part of the client — the navigation management — have to be move to the server. I guess you are saying a File Server is not enough for a website, so please explain **why?** – yves amsellem May 31 '11 at 10:16
  • @yvesamsellem you need the server for a database. You need the server for handling security. [Why you need a server](http://stackoverflow.com/questions/5212859/why-dont-i-just-build-the-whole-web-app-in-javascript-and-javascript-html-templa/5212894#5212894). As for the routing yes you can do it client side but you can't navigate to different physical html pages on a fileserver. You use one html page on the file server and a JSON service. Then there's the whole supporting non-JS users. – Raynos May 31 '11 at 10:22
  • @Raynos Say I'm writing a Twitter client. The server already exists and I'm not in charge of it. Where do I manage the navigation through the pages of my JavaScript client? Does a File Server is enough for that and how to do it? Thanks again. – yves amsellem May 31 '11 at 10:31
  • @yvesamsellem I would set up a small node.js server to serve your web app that loads your twitter client data. In theory you can just server one big html file from a fileserver and it should work. Also best to [join the js chat](http://chat.stackoverflow.com/rooms/17/javascript/) to discuss in greater detail – Raynos May 31 '11 at 10:58