taylorhughes / skit

skit: A pure JavaScript frontend for building better web clients.
MIT License
540 stars 22 forks source link

Deep linking - nice looking urls #8

Open necrodome opened 9 years ago

necrodome commented 9 years ago

I was playing with the rottentomatoes demo. On the main page, there are links to different lists. I tried to provide links to these lists by using server.registerUrlArgument, but couldn't find a way.

So for example, I want /t/in-theaters to match to Home controller with in-theaters matched to a key. In general, is there a way to use named regex params for arbitrary url matching? It would be nice if I could do server.registerUrlArgument('Home', '/t/:list_name) and use query('list_name') in Home controller.

Is there a current way to accomplish this?

taylorhughes commented 9 years ago

Yep, so the way you would do this is by adding a controller at eg. public/t/section/SectionController.js. You don't actually need to use registerUrlArgument() if you're okay with matching any path in section — paths surrounded by underscores in "public" are kind of magic.

Then loading /t/foo will load SectionController, and you can access "foo" by looking at this.params['section'].

taylorhughes commented 9 years ago

(You could probably use a symlink to reference Home.js from /t/section/, but I'm not 100% certain that would work. Probably would. Or you could abstract the common bits to a base controller in /library and call into it from both Home and SectionController.)

necrodome commented 9 years ago

I see. Both solutions seem hacky =) Any reason not to give an option for explicit routing?server.registerUrlArgument seemed like a nice way for out of convention routing.

taylorhughes commented 9 years ago

I'm resisting adding options like that, because then rather than saying "every route corresponds to a directory," you have to say "every route corresponds to a directory, OR a custom route over here" — and everything becomes more complex forever. :) It adds indirection.

The best way to accomplish this in skit would be to inherit from the same base controller in both places, and not define anything new in the new Home and SectionController classes. This is what Home.js and SectionController.js would look like:

var Controller = skit.platform.Controller;
var BaseSectionController = library.BaseSectionController;

module.exports = Controller.create(BaseSectionController, {});

BaseSectionController here is just the existing Home moved to root/library and renamed, then you add the if (this.params['section']) { logic in preload to load the right section.

That should work, does not require new options, and adds no indirection to the project.

juanbrujo commented 9 years ago

+1 to something like dynamic routes. I'l try @taylorhughes proposed solution.

Any clue why rottentomatoes isn't working anymore?

screen shot 2015-04-09 at 17 55 31

taylorhughes commented 9 years ago

@juanbrujo that would happen with skit run rottentomatoes/demo — that project sets up a proxy, so you need to run it with node main.js from the rottentomatoes dir.

juanbrujo commented 9 years ago

dumb me, didn't see the main.js file, tnx,

but still an error: ERROR PROCESSING REQUEST Error: Invalid dependency: "skit.platform.iter" in module: public.Home:js

taylorhughes commented 9 years ago

@juanbrujo https://github.com/clusterinc/skit/issues/9

juanbrujo commented 9 years ago

yep, that's it, tnk a lot, this is smt I was looking for, and congratulations. looking forward for the future of Skit.