Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent m.route.mode #30

Closed
darsain opened this issue Mar 29, 2014 · 1 comment
Closed

Transparent m.route.mode #30

darsain opened this issue Mar 29, 2014 · 1 comment
Labels
Type: Question For issues that are purely questions about Mithril, not necessarily bug reports or suggestions

Comments

@darsain
Copy link
Contributor

darsain commented Mar 29, 2014

If I'm just creating a small app embedded in a website (i.e. a "Chat with us" box in a corner or something), I don't want it to be represented in URL, but I still need the functionality of routes.

Would a transparent m.route.mode be possible? I'd event say it should be the default :)

Or did I missed something in docs?

@lhorie
Copy link
Member

lhorie commented Mar 30, 2014

Mithril's router requires a URL component to tie to (either location.pathname, location.hash or location.search)

With that being said, creating a custom URL-less router is pretty easy:

Let's say you have an app

//your app's namespace
var app = {};

//some modules
app.module1 = { /*...*/ }
app.module2 = { /*...*/ }

You can add an object to hold your routes like this:

//routes to point to said modules
app.routes = {
    "foo": app.module1,
    "bar": app.module2
};

Then you can create a "redirect" method to call programmatically:

//utility to programmatically route
app.route = function(route) {
    m.module(rootElement, app.routes[route])
}

//usage:
app.route("foo") //runs module1

And, optionally, you can create a helper tool to make links routed also

//utility to make links routeable
app.router = function(element, isInit) {
    if (!isInit) {
        m.module(rootElement, app.routes[element.href])
    }
}

//usage:
m("a[href=foo]", {config: app.router})

@lhorie lhorie closed this as completed Apr 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question For issues that are purely questions about Mithril, not necessarily bug reports or suggestions
Projects
None yet
Development

No branches or pull requests

2 participants