Coder Social home page Coder Social logo

Transparent m.route.mode about mithril.js HOT 1 CLOSED

darsain avatar darsain commented on May 22, 2024
Transparent m.route.mode

from mithril.js.

Comments (1)

lhorie avatar lhorie commented on May 22, 2024

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})

from mithril.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.