简体   繁体   中英

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, ie 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

你可以试试BackboneJs,我敢肯定你有几百万个你喜欢的功能http://documentcloud.github.com/backbone/

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.

有一个名为DerbyJS( http://derbyjs.com )的JavaScript框架,可以完全支持您的需求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM