node package manager
Loosely couple your services. Use Orgs to version and reuse your code. Create a free org »

onecode

OneCode Build Status

Description

This web framework tries to get the best from 2 worlds: traditional and single-page websites.

  • Full html code of the page is served to the browser - good for web crawlers, social networks and older browsers.
  • After the page is loaded and shown, the website is transparently switched to single-page mode: no more full-page refreshes, no more html is transmitted over the network, only client-rendered data.
  • The same Controller/View codebase is used both on client AND server, so you don't need to worry to synchronize them. (That's what Node.js promised from the very beginning after all!)
  • Very fast - no browser emulators on server, pure Node.js. Html serving speed is similar to the usual Node.js/Express setups.
  • Built upon widely known technologies - Node.js, Browserify, Backbone.

Installation

$ npm install onecode

Framework

OneCode builds on the single-page website (rich web client) architecture consisting of 2 major parts:

  1. Database and business logic accessible through a well-defined REST API. No source code of this part is visible to client.
  2. View/interaction logic and templates that are sent to the browser. This is javascript and html templates and full source is visible to client.

OneCode helps running the second part both on client and on the server effectively:

  • Unifies access to REST API from both client and server ($.ajax and Backbone.sync).
  • Unifies routing (Backbone.Router/History).
  • Views explicitly separate the html templating (server+client) and dynamic behaviors (client only).
  • After html is generated on server, the page state/data is transparently moved to browser, where it is recreated (all js objects) and dynamic behaviors attached to existing DOM nodes without any html re-rendering or issuing additional requests to the REST API.

Limitations:

  • As the code is executed both on client and server, no globals are allowed (this is a good thing anyways).

Example

We will use CoffeeScript as a safer and more expressive alternative, but this can be written in raw javascript as well.

# This is a main.coffee file, standard Express boilerplate. 
express = require 'express'
browserify = require 'browserify'
onecode = require 'onecode'
 
app = express.createServer()
 
# Make ./client_script/index.coffee (and all dependent modules) accessible to the client as '/main.js' 
app.use browserify mount: 'main.js'entry: './client_script/index.coffee'
 
# Make the same file work in server environment and prepare to serve requests 
# according to Backbone routes. 
app.use onecode require('./client_script/index.coffee')
 
app.listen 3000
# ./client_script/index.coffee: works both on client and server. 
 
OneCode = require 'onecode/client'
OneCode.exportToGlobals() # This makes 'Backbone', '_', 'OneCode', '$' accessible globally. 
 
# The app has everything to render on server and continue working with it on client. 
# It is created on every server request.  
class exports.App extends OneCode.App
    start: ->
        
        
 

(Sample apps: http://addyosmani.github.com/todomvc/)

License: MIT