Tuesday, April 8, 2014

High Level Application Routing with StarLogic, StarMap, and StarShine - An idea in the making

I'm usually against using frameworks, but a few days ago I realized I could actually design a framework on top of StarShine. This framework was a fun idea I came up with while discussing top level routing of an application.

I wanted to make a Superset of the MVC framework, that focuses purely on the Business Logic part of your application, hence the name StarLogic.

This pattern models an event listener pattern, but does it in a functionally reactive way. Take the following example:

var StarLogic = require('StarLogic');
//create a router
var app = StarLogic();
function _newId(bo/*Business Object*/) {
bo.id = new Guid();
}
//if id is falsy, give it a new one
app.addRule({ id: false }, _newId);
//returns a finished business object
app.route({});
//{id: [object Guid]}

The idea with this framework, is to define a bunch of simple rules, and have your business logic execute your functions based on the rules. In the above case, any object that passes through the app.route() function will receive an ID if it isn't set already.

High level application routing can occur when you push a route object to a router like this:

var StarLogic = require('StarLogic');
//create a router
var app = StarLogic();
//fake database module you made
var DB = require('./DB');
function _getUser(route) {
//this is a shared object namespace
this.user = DB.getUser(route.id);
}
function _routeApplication() {
//do some application stuff here like change the view
//do some databinding
this.user;
//is the returned user
}
app.addRule({ controller: "user", id: true, action: true }, _getUser, _routeApplication);

Any object pushed through app will now be checked to see if every rule definition provided is correct, then it will execute _getUser and _routeApplication passing the business object in question to the functions.

Hey wait a minute, what about complex routing and nested routers? Easy. The route function is portable and doesn't rely on this being anything. Here are some examples of where the route function is placed in a proper way.

var eventApp = StarLogic();
var route = eventApp.route;
//every event object will be passed to eventApp
document.getElementById('myElement').keydown = route;
function _42KeyDown (evt) {
evt.preventDefault();
}
//this is a bad idea, but you get the idea
eventApp.addRule({which: 42}, _42KeyDown);
var subRouter = StarLogic();
//some random rule
eventApp.addRule({ bubbles: true }, subRouter.route);
//Now any keydown event that bubbles will be passed to subRouter for execution
//add a rule
subRouter.addRule({ 1:true });
//create a list of things
var items = [];
items.push({1:1}, {2:2}, {3:3});
//push them to a router
items.forEach(subRouter.route);
//now each item will be processed by your defered logic
view raw functional.js hosted with ❤ by GitHub

Essentially you get to design data processes, define application level logic, and design a framework for your application on top of StarLogic. Move over angular, StarLogic is coming to a store near you.

No comments:

Post a Comment