Giter VIP home page Giter VIP logo

chinamvc's Introduction

China MVC

A simple MVC framework for node.js.

Features

  • RESTful routes
  • Customized routes
  • Support all kinds of view engines supported by Express, default of which is Jade
  • View templates can be stored in all kinks of storage
  • Sub views

Dependence

ChinaMVC depends on the following node.js packages, you can download them use 'npm' command.

  • Express: a framework for fast build a web server via node.js

Directory Rules(for defalt routes)

./
./app.js
./controllers/
./controllers/$controller/
./controllers/$controller/$action/
./controllers/$controller/$action/$httpmethod.js
./views/
./views/$controller/
./views/$controller/$action.jade
./views/$controller/$action/$subview.jade

HOW TO USE

RESTful Web/API

// app.js
// RESTfull routes
// SELECT
app.get(/.+\/.*/, function(req, res) {
    (new mvc.context(req, res)).invoke();
});

// INSERT
app.post(/.+\/.*/, function(req, res) {
    (new mvc.context(req, res)).invoke();
});

// DELETE
app.del(/.+\/.*/, function(req, res) {
    (new mvc.context(req, res)).invoke();
});

// UPDATE
app.put(/.+\/.*/, function(req, res) {
    (new mvc.context(req, res)).invoke();
});

Customized route

// app.js

// route for homepage, controller is 'home' and view is 'index'
app.get('/', function(req, res) {
    (new mvc.context(req, res)).invoke('home', 'index');
});

Using Sub Views

// a controller, e.g. /home/index/get.js
var subViews = ['view1', 'view2', 'view3'];
exports.render = function (context) {
    var subView = subViews[Math.floor(Math.random()*100)%3];
    context.view({ layout: false }, subView);
};

Store View Templates in Databases(or any other storage)

// This feature is very useful for CMS ect.
// callback(err<exception>, template<string>)
function getTemplate(id, callback){
    var template = '!!! 5'; // TODO: get template content from storage.
    callback && callback(null, template);
}
exports.render = function (context) {
    getTemplate(context.request.params['id'], function(err, template){
        var render = jade.compile(template);
        
        // view model
        var vm = {};
        
        context.response.send(render({ vm: vm }));
    });
};

chinamvc's People

Contributors

lchrennew avatar

Stargazers

256 avatar

Watchers

James Cloos avatar 256 avatar

Forkers

patipati

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.