Multi-player Notepad: A walkthrough of creating a new project with derby.js
Completed game:
https://github.com/josephg/multiplaye...
"godbox" livedb data administration:
https://github.com/share/godbox
http://derbyjs.com
http://sharejs.org
sudo apt-get install node
wget http://nodejs.org/dist/v0.10.12/node-v0.10.12-linux-x64.tar.gz
tar -xvf node-v0.10.12-linux-x64.tar.gz
cd node-v0.10.12-linux-x64
./configure --prefix=/home/alias/Applications/node/
make install
echo 'export PATH=$PATH:/home/alias/Applications/node/bin' >> ~/.bashrc
wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
tar -xvf redis-2.6.14.tar.gz
make -j 6
make test
make PREFIX=/home/alias/Applications/redis/ install
echo 'export PATH=$PATH:/home/alias/Applications/redis/bin' >> ~/.bashrc
cd utils
./install_server
/home/alias/Applications/redis/bin/redis-server
sudo apt-get install mongodb-server
/home/alias/Applications/redis/bin/redis-server
sudo /etc/init.d/mongodb restart
npm install -g derby
derby new SocialReport
npm start
lib
styles
ui
views
package.json
server.js
// Derby routes are rendered on the client and the server
app.get('/', function(page) {
page.render('home');
});
$ npm install -g derby
$ derby -V
project/
-package.json
-index.js
-derby-app.js
views/
derby-app.html
styles/
derby-app.less
{
"name": "DerbyTutorial",
"description": "",
"version": "0.0.0",
"main": "./server.js",
"dependencies": {
"derby": "*",
"express": "3.x"
},
"private": true
}
npm install
which will download our dependencies intonode_modules folder.views/derby-app/index.html
, or be inside of a file which has the same name as your derby app JS file, i.e., derby-app.html.<Body:>
template and{message}
variable. Derby uses mustach-handlebars-like syntax for reactive binding. index.html looks like this:<Body:>
<input value="{message}"><h1>{message}</h1>
h1 {
color: blue;
}
require()
function:var http = require('http'),
express = require('express'),
derby = require('derby'),
derbyApp = require('./derby-app');
var expressApp = new express(),
server = http.createServer(expressApp);
var store = derby.createStore({
listen: server
});
var model = store.createModel();
expressApp.
use(store.modelMiddleware()).
use(express.static(__dirname + '/public')).
use(derbyApp.router()).
use(expressApp.router);
server.listen(3001, function(){
model.set('message', 'Hello World!');
});
var http = require('http'),
express = require('express'),
derby = require('derby'),
derbyApp = require('./derby-app');
var expressApp = new express(),
server = http.createServer(expressApp);
var store = derby.createStore({
listen: server
});
var model = store.createModel();
expressApp.
use(store.modelMiddleware()).
use(express.static(__dirname + '/public')).
use(derbyApp.router()).
use(expressApp.router);
server.listen(3001, function(){
model.set('message', 'Hello World!');
});
app.ready()
callback. To start, let’s require and create an app. Derby uses unusual construction (not the same familiar good old module.exports = app
):var derby = require('derby'),
app = derby.createApp(module);
/
, a.k.a. root):app.get('/', function(page, model, params) {
model.subscribe('message', function() {
page.render();
})
});
var derby = require('derby'),
app = derby.createApp(module);
app.get('/', function(page, model, params) {
model.subscribe('message', function() {
page.render();
})
});
node .
ornode index.js
and open a browser at http://localhost:3001. You should be able to see something like this: http://cl.ly/image/3J1O0I3n1T46.store.afterDb
to listen to set
events onmessage
attribute:server.listen(3001, function(){
model.set('message', 'Hello World!');
store.afterDb('set','message', function(txn, doc, prevDoc, done){
console.log(txn)
done();
})
});
var http = require('http'),
express = require('express'),
derby = require('derby'),
derbyApp = require('./derby-app');
var expressApp = new express(),
server = http.createServer(expressApp);
var store = derby.createStore({
listen: server
});
var model = store.createModel();
expressApp.
use(store.modelMiddleware()).
use(express.static(__dirname + '/public')).
use(derbyApp.router()).
use(expressApp.router);
server.listen(3001, function(){
model.set('message', 'Hello World!');
store.afterDb('set','message', function(txn, doc, prevDoc, done){
console.log(txn)
done();
})
});
model.on()
to app.ready()
: app.ready(function(model){
model.on('set', 'message',function(path, object){
console.log('message has been changed: '+ object);
})
});
var derby = require('derby'),
app = derby.createApp(module);
app.get('/', function(page, model, params) {
model.subscribe('message', function() {
page.render();
})
});
app.ready(function(model) {
model.on('set', 'message', function(path, object) {
console.log('message has been changed: ' + object);
})
});