4 ms·
If you were previously looking at integrating Backbone.js with Sammy, jQuery-BBQ, or another hashchange-based library, there's no longer any need. Instead:
by jashkenas 16y ago
If you were previously looking at integrating Backbone.js with Sammy, jQuery-BBQ, or another hashchange-based library, there's no longer any need. Instead:
var Workspace = Backbone.Controller.extend({
routes:{
"search/:query/p:page": "search",
"folder/:name": "openFolder",
},
search: function(query, page) {
...
},
openFolder: function(name) {
...
}
});
Which will properly route URLs like "example.com/#search/lemons/p7"
- swah 16y agoDoes the critic that you made to Sammy applies to Backbone? :) Link: http://news.ycombinator.com/item?id=1788631 http://news.ycombinator.com/item?id=1788631
- jashkenas 16y agoI presume you're referring to this: http://news.ycombinator.com/item?id=1882476 http://news.ycombinator.com/item?id=1882476 Pieces of the critique definitely apply to this release: I'm still of the opinion that routing is a relatively small portion of a client-side app. And you'll probably wind up with 10x more Views, Models, and Collections than you do Controllers. The big difference between this approach and Sammy's is that here Controllers are just a library you can use -- with Sammy you structure your whole app around inappropriate faux-server-side-URLs-with-HTTP-methods. (Also, this works in Internet Explorer.) In the end, enough people requested we add routing to Backbone that it made sense to plug it in, one more battery included.
- siong1987 16y agois there any way to change the url from "/#" to "/#!" so that it can be crawlable by google bot?
- jashkenas 16y agoAt the moment, you can do it by including "!" at the start of your routes. Perhaps it should be a configurable option... We talked about it a bit, and the consensus was that "/#!/" URLs were a temporary hack that is obsolete now that "pushState" and "replaceState" are part of HTML5 -- you can now mint real URLs that are crawled by Google and are accessible from single-page apps as well.
- swah 16y agoAdded link to the comment about Sammy. Basicly it said you shouldn't organize the whole app around faux-routes. Didn't understand the downvotes.
- cpr 16y agoJust a nit: is that "search/:query/:page"?
- mrduncan 16y agoI don't think so, "example.com/#search/lemons/p7" would route to search("lemons", "7").
- jashkenas 16y agoYou could do either, but in this case, I'm going for URLs like: example.com/#search/washington+news/p10 ... to make it a bit more obvious that the number at the end is a page number. This is actually a real-world example -- it's the same route that DocumentCloud uses to run searches in the workspace.
- deleted 16y ago[deleted]
- bergie 16y agoActually, why use colons instead of URI templates? http://tools.ietf.org/html/draft-gregorio-uritemplate-04 http://tools.ietf.org/html/draft-gregorio-uritemplate-04
- jashkenas 16y agoHey, that's a great idea. I've been following the progress of that draft a good bit, and we're already using URI-templates as part of the DocumentCloud API... "image":"http://s3.dcloud.org/docs/101/pages/inauguration{page}-{size}.gif" I'd be a bit concerned if we used them in Backbone, that we wouldn't be following the spec by parsing all variants of the expansions -- but on balance, it's probably a great thing to do.
- bergie 16y agoCool. We use a URL patterns variant in Midgard MVC, so that should make Backbone integration easier :-) http://github.com/bergie/midgardmvc_core http://github.com/bergie/midgardmvc_core