/genie

Primary LanguageJavaScriptMIT LicenseMIT

genie

Build Status

Genie is framework for Express3.x based to Klass.

Installation

Get the CLI

$ npm install genie-express -g

Quick Start

Create an app

$ genie create my-app
$ cd my-app $$ npm install genie-express

Start your app

$ node server.js

Scaffold

$ genie controller [name]
$ genie model [name]
$ genie service [name]
$ genie factory [name]

Examples

Controller

module.exports = function (app, config) {
  var ApplicationController = app.getController("Application", true);
  var IndexController = ApplicationController.extend(function(){
    this.routes = {
      "index" : "/"
    };
    this.addBeforeAction("action", function(res, req, next){
      var CheckFactory = app.getFactory("Check");
      CheckFactory.start(res, req, next);
    });
    this.addBeforeAction("get", function(res, req, next){
      var CheckFactory = app.getFactory("Check");
      CheckFactory.start(res, req, next);
    });
    this.addBeforeAction("get", function(res, req, next){
      var CheckService = app.getService("Check");
      CheckService.start(res, req, next);
    });
  }).methods({
    getIndex : function(req, res){
      res.render("index", {
        title : "Hello, Genie"
      });
    },
  });
  return IndexController;
}

Model

'use strict';
module.exports = function(app){
  var ApplicationModel = app.getModel('Application', true);
  var CheckModel = ApplicationModel.extend(function(){
    this.app = app;
    this.modelName = "Check";
    this.rooms = ['Apple', 'Orange', 'Grape'];
  }).methods({
    getRoom : function(id){
      console.log(this.rooms);
      return this.rooms[id];
    }
  });
  return CheckModel;
};

Service

'use strict';
module.exports = function(app){
  var ApplicationService = app.getService('Application', true);
  var CheckService = ApplicationService.extend(function(app){
    this.app = app;
    this.serviceName = "Check";
    this.config = this.Config();
    this.num = this.config.num;
  }).methods({
    start : function(req, res, next){
      this.supr();
      console.log("service: "+ this.num++);
      next();
    }
  });
  return CheckService;
};

Factory

'use strict';
module.exports = function(app){
  var ApplicationFactory = app.getFactory('Application', true);
  var CheckFactory = ApplicationFactory.extend(function(){
    this.factoryName = "Check";
    this.num = 0;
  }).methods({
    start : function(req, res, next){
      this.supr();
      console.log("factory: "+ this.num++);
      next();
    }
  });
  return CheckFactory;
};

View

Uses Twig to render Template

<div id="title">{% block content %}{% endblock %}</div>
{% extends "layout.twig" %}
{% block content %}
{{ title }}
{% endblock %}

Routing

Routings are extract from method of controller.
Extract methods are GET, POST, PUT and DELETE.

get /index

getIndex : function(req, res){
  res.render("index", {
    title : "Get index"
  });
}

post /create

postCreate : function(req, res){
  res.render("index", {
    title : "Post create"
  });
}

Params are extract from property of controller.

get /test/member/:id

// controllers/test/TestController.js

module.exports = function (app, config) {
  var ApplicationController = app.getController("Application", true);
  var TestController = ApplicationController.extend(function(){
    this.params = {
      "member" : [":id"]
    };
  }).methods({
    getMember : function(req, res){
      res.render("index", {
        title : "Genie is "+req.params.id
      });
    },
  });
  return TestController;
}

In the case of a different method?

this.params = {
  "member" : {
    get : [":id"],
    post: [":test"]
  }
};

Routes change extract from property of controller.

get /test/

// controllers/test/TestController.js

module.exports = function (app, config) {
  var ApplicationController = app.getController("Application", true);
  var TestController = ApplicationController.extend(function(){
    this.routes = {
      "index": "/test/"
    };
  }).methods({
    getIndex : function(req, res){
      res.render("index", {
        title : "Genie is test"
      });
    },
  });
  return TestController;
}

Service and Factory

Service will create an instance for each call.

module.exports = function (app, config) {
  var ApplicationController = app.getController("Application", true);
  var TestController = ApplicationController.extend(function(){
    this.TestService = this.app.getService("Test");
  })
}

Factory will call the same instance.

module.exports = function (app, config) {
  var ApplicationController = app.getController("Application", true);
  var TestController = ApplicationController.extend(function(){
    this.TestFactory = this.app.getFactory("Test");
  })
}