spirit-js / spirit

Modern modular library for building web applications
http://spirit.function.run
ISC License
244 stars 18 forks source link

Feature request. URL reversing function #4

Closed kokujin closed 8 years ago

kokujin commented 8 years ago

It would be nice if Spirit had this( url_for, urlFor, pathFor...)

hnry commented 8 years ago

I'm not sure I understand, can you show what the input is and what the output would be so I have better idea?

kokujin commented 8 years ago

Hi there, sorry for the delay. I meant actually an equivalent for "named routes". In some frameworks, there might be a function that generates a route based on its name, for example, "url_for()" or "pathFor()".

There are some modules that try to implement this in express, but I am yet to see one that works properly https://www.npmjs.com/search?q=named-routes

Was that a bit clearer?

hnry commented 8 years ago

thanks for the suggestion, if i understand right, then it's not needed since spirit-router does away with routes that are so tied into the routing, that's one of the main emphasis.

So for example a function that returns the query of a database:

const querydb = (id) => {
  return userdb.findOne({ id: id })
}

Can be used as a route as-is:

const route = require("spirit-router")

const app = route.define([
  route.get("/users/:id", ["id"], querydb)
])

But you can use the "route" (it's just a plain js function) on it's own:

querydb(123)

So you don't need a way to "link" or name the route to re-use it.

If you wanted to simulate the route being called through the router (not sure why it would be needed):

app({ method: "GET", url: "/users/123" }) // this returns back the same as querydb(123)

99% of spirit is just normal JS functions, as you can see define returns a JS function (that was assigned to 'app') that you can call too without req or res. And it returns back a value too.

But again I would just call your route function (querydb).

The difference between spirit and other frameworks is... your functions can be used as routes and not the other way around where your routes are some sort of proprietary routing functions.

If you wanted to do an actual http request then you just use a http client request library.

I hope this makes sense, and if I'm still missing the point please let me know.

kokujin commented 8 years ago

Not quite...one of the main reasons is to prevent guesswork and hardcoding URLs in templates

hnry commented 8 years ago

Ah ok! that makes sense, but it won't ever be included into spirit or the router since it's meant to be more light and not provide everything a full framework might.

So I will leave that up to a 3rd party library or some framework that extends spirit to have that feature.

For those curious on how to implement, they can write function that wraps over define, and basically just proxies it, and internally they can have their own cache of names.

godDLL commented 7 years ago

If one wants to do a routeFor('somepath') inside a template, she could use dependency injection to supply that route fn as a local to template context, as with every other value in templates.