Sri
Sri

Reputation: 5845

Unique URLs for single page JavaScript apps?

How do you generate unique URLs for a single page JavaScript app.

The URLs should behave like a normal URL, i.e. paste it in the browser and the app should load with that specific view showing provided the user has permissions. Gmail does this, so do many others.

I'm guessing it will involve reading the location hash value and performing actions appropriately.

For future projects, are there any frameworks out there that give this feature built in?

Thanks, Sri

Upvotes: 1

Views: 1161

Answers (3)

yannisgu
yannisgu

Reputation: 493

There is a JavaScript Framework called DerbyJS ( http://derbyjs.com ) which support exactly what you want.

Upvotes: 0

Owen Allen
Owen Allen

Reputation: 11958

I would not build it on hash values. Use history.pushState and such to build it with real natural URLs. Using rewrites have a request to ANY normal resource on the site be processed by a single processor. Then onload, simply check what's in the URL against some sort of hash which contains the controller to build that resource. In addition, you can build the URLs, so they are actually regex values to allow for semi-dynamic URLs, in a one page scenario.

urls = {
  { url : "/content/id/(\d)*?/", controller : controllers.blah }
  { url : "/home/", controller : controller.home }
}
controllers : {
  blah : function() {
    // do stuff
  },
  home : function() {
    // do stuff
  }
}

Obviously it gets a lot more complex, but this is the approach I used to create my javascript one pager. Also, on page load I would run a jQuery call which has all internal link clicks to run through the exact same processor that the page uses on page load, effectively hijacking them.

Upvotes: 1

Milan Jaric
Milan Jaric

Reputation: 5646

You can try BackboneJs, I'm sure there are million of features you'd like http://documentcloud.github.com/backbone/

Upvotes: 3

Related Questions