Created
March 16, 2013 09:44
-
-
Save fritz-gerneth/5175698 to your computer and use it in GitHub Desktop.
Suggested router API/Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MVCRoute | |
{ | |
public function getController(); | |
} | |
class MVCDispatcher | |
{ | |
var template = router.match(request).getController(); | |
// render template into view, or pass it to view-package, ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
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
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.
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.