UNPKG

5.41 kBJavaScriptView Raw
1// Backbone Server
2// Nate Hunzaker
3//
4// Adds a module for server side interaction within Backbone using Express
5
6
7//-- Requirements ------------------------------------------------------//
8
9// Side Dishes
10require('colors');
11
12var _ = require('underscore'),
13 express = require('express'),
14 ejs = require('ejs'),
15 fs = require('fs');
16
17// The Main Course
18module.exports.Backbone = Backbone = require('backbone');
19
20
21//-- Backbone Server ---------------------------------------------------//
22Backbone.Server = function (options) {
23
24 //-- Default Values ------------------------------------------------//
25 var settings = {
26 'database' : false,
27 'express' : express.createServer(),
28 'port' : 8000,
29 'public' : 'public',
30 'routes' : {},
31 'socketio' : false,
32 'views' : 'views',
33 'view engine' : 'ejs' // We use EJS as the default, simply because it has good synergy with natively with backbone
34 };
35
36 // Add Default Overides
37 _.extend(this, settings, options);
38
39 // Set everything else up
40 this._setup();
41};
42
43
44// Backbone Server Functionality
45_.extend(Backbone.Server.prototype, Backbone.Events, {
46
47 //-- Modules ------------------------------------------------------//
48
49 // A nifty helper to load all files within a directory
50 // Note : This is basically a glorified "require all in directory" function.
51 // Should further releases of node include such functionality, it will
52 // be deprecated.
53 load: function(module) {
54
55 var dir = require.main.filename.split("/").slice(0, require.main.filename.split("/").length - 1).join("/"),
56 files = fs.readdirSync( dir + "/" + module, 'utf-8');
57
58 files.forEach(function(file) {
59 require( dir + "/" + module + "/" + file );
60 });
61
62 },
63
64 //-- Standard CRUD Routing Methods --------------------------------//
65
66 route: function (method, path, action) {
67 this.routes[path] = [method, action];
68 },
69
70 get: function(path, action) {
71 this.route('get', path, action);
72 },
73
74 post: function(path, action) {
75 this.route('post', path, action);
76 },
77
78 put: function(path, action) {
79 this.route('put', path, action);
80 },
81
82 delete: function(path, action) {
83 this.route('delete', path, action);
84 },
85
86 _setup: function() {
87
88 var server = this;
89
90 // Set static content folder
91 this.express.use(express.static(this.public));
92
93 // Set the view engine
94 this.express.set('view engine', this['view engine']);
95
96
97 //-- SocketIO Methods -----------------------------------------//
98
99 if ( this.socketio ) {
100
101 this.io = require('socket.io').listen(this.express);
102
103 // A syntax sugar for socketIO
104 this.bind = function(name, action) {
105 this.io.sockets.on(name, action);
106 };
107
108 this.send = function(name, values) {
109 this.io.sockets.emit(name, values);
110 };
111
112 this.events = function(events) {
113
114 for (var e in events) {
115 this.bind(e, events[e]);
116 }
117
118 };
119
120 console.log("Socket.IO engaged.".blue);
121 }
122
123 //-- Mongo Methods --------------------------------------------//
124
125 if (this.database.adapter === 'mongo') {
126
127 var Schema = require('./backbone-mongo').Schema;
128
129 require('./backbone-mongo').setup(this.database);
130
131 this.Datamodel = {
132 mongoose: require('./backbone-mongo').mongoose
133 };
134
135 this.Datamodel.register = function(name, schema) {
136
137 if (!name) {
138 throw new Error("\nPlease provide a name for this MongoDB model.\n".red.bold)
139 }
140 if (!schema) {
141 throw new Error("\nPlease provide a schema for ".red.bold + name.red.bold + ".\n".red.bold);
142 }
143
144 var new_schema = new Schema(schema);
145
146 server.Datamodel[name] = mongoose.model(name, new_schema);
147 };
148
149 }
150 },
151
152 //-- Server Methods ------------------------------------------------//
153
154 start: function() {
155
156 // Add routes
157 for (var i in this.routes) {
158
159 var method = this.routes[i][0],
160 action = this.routes[i][1];
161
162 this.express[method](i, action);
163 }
164
165 // Add a route to load backbone:
166 this.express.get('/backbone-server/backbone.js', function(req, res) {
167
168 var underscorejs = fs.readFileSync(__dirname + '/node_modules/underscore/underscore-min.js', 'utf-8'),
169 backbonejs = fs.readFileSync(__dirname + '/node_modules/backbone/backbone.js', 'utf-8');
170
171 res.contentType('text/javascript');
172 res.send(underscorejs + "\n" + backbonejs);
173
174 });
175
176 this.express.listen(this.port);
177
178 console.log("Backbone Server is listening on port ".blue + this.port.toString().yellow);
179 }
180
181});
\No newline at end of file