Skip to content

Instantly share code, notes, and snippets.

@fritz-gerneth
Created March 16, 2013 09:44
Show Gist options
  • Save fritz-gerneth/5175698 to your computer and use it in GitHub Desktop.
Save fritz-gerneth/5175698 to your computer and use it in GitHub Desktop.
Suggested router API/Interface
class MVCRoute
{
public function getController();
}
class MVCDispatcher
{
var template = router.match(request).getController();
// render template into view, or pass it to view-package, ...
}
The request and the router are decoupled. For the router it doesn't matter from where the current URI to match is actually from. This enables server-side routing for the same setup as well. (Using a different dispatcher of course). On a long-term the request probably would be a package itself abstracting client/server differences in a request.
The dispatcher is depending on the `doesMatch` method, and trigger recalculation of the view uppon invalidation. As we receive a callback we can invalidate controllers (being destructors for those, to unsubscribe/....).
The implementation of the routes doesn't matter for the dispatchers, why should they care about how the data was gathered.
/**
* Common interface for any kind of routes
*/
interface RouteInterface
{
/**
* Does this route match the request
*
* @reactive
*/
public function doesMatch(request);
/**
* Match the route for the request
*
* @return RouteMatch
*/
public function match(request);
/**
* Assemble a URI according matching this route (similiar to "to")
*/
public function assemble(params = array());
}
interface RouteStackInterface
{
public function addRoute(RouteInterface);
public function doesMatch(request);
public function match(request);
}
/**
* Data container for information about the current route match.
* Changes in here do not trigger rebuild of the actual URI.
* This object is not reactive (as the layer above should be).
*/
class RouteMatch
{
/**
* Constructor
*/
public RouteMatch(params);
/**
* Set the matching route
*/
public function setMatchingRoute(RouteInterface route);
/**
* Get the matching route
*/
public function getMatchingRoute();
/**
* Params are those matched, i.e :id, :controller, ...
* routeMatch->getParam("id", 5);
*/
public function setParam(name, value);
public function getParams();
public function getParam(name, default = null);
}
For pagejs to direct navigation this might be:
class PageJSRouter
{
// implementation
}
var router = RouteStack();
router.addRoute(...);
// another package
class SimpleDispatcher
{
var matchObj = router.match(request);
// render matchObj.getParam('template');
}
@tmeasday
Copy link

tmeasday commented Apr 1, 2013

Hi @fritz-gerneth (did that make you get an email?).

Sorry again for my slow response!

I think I'm understanding what you are saying, but I'm confused about two things.

  1. I'm not sure what a Front Controller is, but I think you are saying it makes a certain function run every time a certain URL matches?
  2. It's not clear in all this how the actual active window (or request)'s URL (which has to be a singleton) is involved.

I've started to write up a "split" API based on your ideas here: https://github.com/tmeasday/meteor-router/wiki/Split-API

What do you think? The concepts match across pretty cleanly to what you are saying here if I've understood you correctly.

@fritz-gerneth
Copy link
Author


var FrontController = function (routeStack, dispatcher) {
    "use strict";

    Meteor.autorun(function () {
        var currentRoute = routeStack.match();
        // pull information on what to call from where-ever, e.g. a dispatcher or for simplicity, from the route itself
        currentRoute.controller();
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment