分享 leading slashes won't play well with pushState?

cqpx · 2012年03月01日 · 最后由 lainuo 回复于 2012年03月01日 · 3947 次阅读
jashkenas commented 2 months ago
Backbone should be prescriptive here because leading slashes won't play well with pushState.

https://github.com/documentcloud/backbone/issues/849

请问他这么说有什么依据么。。

用惯了 spine 再来用 backbone 觉得它的结构好混乱。。

我去 IRC 问了...

The topic for #documentcloud is: Discussion for DocumentCloud, Backbone.js, Underscore.js, Jammit, etc. DocumentCloud.org users, don't hesitate to ask jashkenas or knowtheory for help. (1:16:30 PM)
1:18:02 PM winfield: Hi, all. I have a question: why "you should never be using leading slashes with Backbone routes" ? 
1:18:29 PM tbranyen: bad stuff may happen
1:18:43 PM dator: computer explodes
1:19:20 PM winfield: tbranyen: what kind of `bad stuff` ? 
broofa left the room (quit: Ping timeout: 252 seconds). (1:19:46 PM)
danzik17 left the room (quit: Ping timeout: 276 seconds). (1:20:06 PM)
1:20:25 PM tbranyen: winfield: like your routes won't trigger and you'll wonder why
1:20:33 PM tbranyen: and then you'll realize you shouldn't have done that
1:21:40 PM winfield: tbranyen: I understand this, but why not a leading slash in router. Twitter has the leading slash
1:22:59 PM paul_irish: Wait is that saying   example.com/app/#/dashboard   is bad bad?
1:23:15 PM jvdev: http://www.webpronews.com/twitter-hashbang-urls-will-soon-be-a-thing-of-the-past-2012-02
1:23:25 PM tbranyen: winfield: pushstate woes
1:23:35 PM tbranyen: so it gets stripped now and won't match your routes
1:23:40 PM tbranyen: iirc
1:25:01 PM tbranyen:   // Cached regex for cleaning leading hashes and slashes .
1:25:15 PM tbranyen: so yeah routeStripper appears to do that
1:25:45 PM paul_irish: but pushState is opt-in
1:25:55 PM winfield: tbranyen: could you explain to me a litter bit more, why leading stash doesn't get long with pushstate
braddunbar [[email protected]] entered the room. (1:26:50 PM)
austinbv [[email protected]] entered the room. (1:26:56 PM)
gauzmps left the room (quit: Ping timeout: 245 seconds). (1:27:08 PM)
1:28:46 PM tbranyen: paul_irish: ya looks like it'll only run that regex on pushstate
1:28:59 PM tbranyen: i guess i've always been extra cautious and kept my routes ambiguous
1:29:17 PM paul_irish: but
1:29:41 PM paul_irish: if i pushState({},'', '/omgrootpath/thing');
1:30:00 PM paul_irish: nothing breaks unless I don't support that URL
bobspryn [[email protected]] entered the room. (1:30:15 PM)
1:30:31 PM paul_irish: in pushState mode, you're signing up for all sorts of URL support that you dont need to worry about with #s
louissmit left the room (quit: Quit: louissmit). (1:30:36 PM)
1:30:51 PM paul_irish: but leading slashes is just one of those things you make a call about
1:31:24 PM paul_irish: i guess here's a usecase that winfield would find legit...
1:31:57 PM paul_irish: i just linked you to view a shoe on my store .. it's actually a SPA but it's got sweet URLs
1:32:17 PM paul_irish: shoestore.com/view/SH324234/awesome-bling-gold-sneaks/
1:32:23 PM paul_irish: you click the home logo
1:32:29 PM paul_irish: shoestore.com/home
1:32:46 PM paul_irish: You need leading slashes in that pushState
1:33:19 PM tbranyen: when you navigate you would use leading yeah
1:33:34 PM tbranyen: i'm testing right now if a matched route would work with leading
1:33:39 PM tbranyen: i don't think it will
1:34:01 PM paul_irish: ah. merely matching the routes..
joshontheweb [[email protected]] entered the room. (1:34:08 PM)
broofa [[email protected]] entered the room. (1:34:09 PM)
1:34:22 PM dator: Could anyone tell me why Im getting a error from underscore with this code? (TypeError: 'undefined' is not an object (evaluating 'func.bind')) Code => http://pastebin.com/3r0tQbqg
1:34:49 PM tbranyen: yeah breaks on empty route too
1:34:56 PM tbranyen: "" is needed, not "/"
1:35:08 PM jvdev: dator: no 'unrender' method
1:35:34 PM tbranyen: thats with hash too
1:35:38 PM paul_irish: without leading you woudlnt be able to differentiate between x.com/dashboard and x.com/support/dashboard
1:35:49 PM dator: jvdev: I took that out and replaced it with remove, but remove is a built in function right? so do i need to bother with binding a built in function?
1:35:53 PM paul_irish: in routes
1:37:11 PM winfield: so it's just a personal preference in Backbone ?
1:37:34 PM winfield: with no leading slash in router
austinbv left the room (quit: Quit: austinbv). (1:37:43 PM)
1:37:44 PM tbranyen: hrm
1:37:47 PM dator: jvdev: thank you btw
1:37:49 PM jvdev: dator: depends what's calling it. if it's only being used in the scope of the class, no
1:38:10 PM tbranyen: paul_irish: i don't think thats a problem with backbone, support/dashboard would superscede it and dashboard on its own wouldn't match
1:38:29 PM tbranyen: er precede it
1:38:41 PM paul_irish: ok
1:38:50 PM paul_irish: then i have no more arguments!
1:38:56 PM paul_irish: death to leading slashes in routes!
1:39:01 PM tbranyen: lol if you want to be safe
1:39:10 PM tbranyen: i was using them before in this client project that was using the #!
1:39:20 PM kmiyashiro: backbone .9 took out preceeding slash support I think, probably wouldn't be hard to patch it back in if you need it
1:39:33 PM tbranyen: since thats what the existing app did
kmiyashiro left the room (quit: Quit: kmiyashiro). (1:40:53 PM)
njero left the room (quit: Read error: Connection reset by peer). (1:41:06 PM)
njero [[email protected]] entered the room. (1:41:23 PM)
shaunxcode left the room (quit: Ping timeout: 252 seconds). (1:41:51 PM)
1:42:33 PM tbranyen: ah heres another big deal with hash and pushState
1:42:50 PM tbranyen: if you navigate with leading slashes with hash, #/my_route and send that link to someone who has pushState
1:42:53 PM tbranyen: http://127.0.0.1:8000//dashboard
1:42:58 PM tbranyen: thats what it converts to
1:43:11 PM tbranyen: which is probably unintended and won't match routes correctly
1:44:49 PM tbranyen: winfield: i'd say its only preference, if you always intend on using hash and you make sure to bind "" and "/" to your routes for the index
efazati left the room (quit: Read error: Operation timed out). (1:45:26 PM)
1:46:32 PM winfield: tbranyen: i hate hash…  just try to understand why leading slash doesn't play well with pushState
1:46:38 PM tbranyen: see above
1:47:15 PM winfield: tbranyen: yes, it makes sense 
1:47:22 PM tbranyen: if you use a leading slash and your user has only hash support and shares http://mylink.com#/leading it gets converted to http://mylink.com//leading with pushState enabled browsers
1:48:31 PM tbranyen: thx for the questions tho paul_irish, i want to write this up in the boilerplate docs
1:49:01 PM tbranyen: i knew there were reasons why i played it safe
需要 登录 后方可回复, 如果你还没有账号请 注册新账号