Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backbone-server

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-server - npm Package Compare versions

Comparing version 0.2.2 to 0.3.0

.gitignore

60

example/example.js

@@ -1,19 +0,57 @@

// Example Server
// Name : Example Server
// Author : Nate Hunzaker
//
//-- 1. Create Backbone.Server
//-- 2. Collect Data
//-- 3. Routes
//-- 4. Socket.io Methods
//-- 5. Start Server
//
// Require backbone
var Backbone = require('../lib/backbone-server');
//-- 1. Create Backbone.Server
var Backbone = require('../lib/backbone-server');
var server = new Backbone.Server({
'port' : 9000,
'socketio' : true,
'view engine' : 'ejs'
});
// Create a new server instance
var server = new Backbone.Server();
//-- 2. Collect Data
var articles = require('./data.js').article_data;
// Add routes
server.get('/', function(req, res) {
res.send("Hello, world!");
});
//-- 3. Routes
// Start server
server.start();
// Index
server.get('/', function(req, res) {
res.render("index");
});
// Single Article Page
server.get('/:name', function(req, res) {
var article = articles.get(req.params.name).toJSON();
res.render('article', article);
});
// For partial rendering on the client side
server.get('/template/:name', function(req, res) {
res.sendfile(__dirname + '/views/' + req.params.name + "." + server['view engine']);
});
//-- 4. Socket.io Functions
server.receive('connection', function() {
console.log("Received connection, sending articles...");
server.send('articles', articles);
});
//-- 5. Start Server
server.start();

62

lib/backbone-server.js

@@ -9,5 +9,9 @@ // Backbone Server

var Backbone = require('backbone');
express = require('express');
var Underscore = require('underscore');
Backbone = require('backbone');
express = require('express'),
ejs = require('ejs');
fs = require('fs');
var _ = Underscore;

@@ -23,5 +27,6 @@ //-- Backbone Server ---------------------------------------------------//

'views' : 'views',
'view-engine' : 'jade',
'view engine' : 'jade',
'routes' : {},
'app' : express.createServer(),
'express' : express.createServer(),
'socketio' : false,

@@ -33,13 +38,29 @@

params || (params = {});
// Merge in custom properties
_.extend(this, params);
this.routes = params['routes'] || this.routes;
this.port = params['port'] || this.port;
// Set static content folder
this.express.use(express.static(this.public));
this.app.use(express.static(this.public));
this.app.set('view engine', this['view-engine']);
// Set the view engine
this.express.set('view engine', this['view engine']);
//-- SocketIO Methods -----------------------------------------//
if ( this['socketio'] ) {
this.io = require('socket.io').listen(this.express);
// A syntax sugar for socketIO
this.receive = function(name, action) {
this.io.sockets.on(name, action);
}
this.send = function(name, action) {
this.io.sockets.emit(name, action);
}
}
},
//-- Routing Methods Values ----------------------------------------//
//-- Standard CRUD Routing Methods --------------------------------//

@@ -70,3 +91,3 @@ route: function (method, path, action) {

start: function() {
// Add routes

@@ -78,7 +99,20 @@ for (var i in this.routes) {

console.log("Route created for " + i);
this.app[method](i, action);
this.express[method](i, action);
}
this.app.listen(this.port);
// Add a route to load backbone:
this.express.get('/backbone-server/backbone.js', function(req, res) {
console.log('hi');
var underscorejs = fs.readFileSync(__dirname + '/node_modules/underscore/underscore-min.js', 'utf-8'),
backbonejs = fs.readFileSync(__dirname + '/node_modules/backbone/backbone.js', 'utf-8');
res.contentType('text/javascript');
res.send(underscorejs + "\n" + backbonejs);
});
this.express.listen(this.port);
console.log("Backbone Server is listening on port " + this.port);

@@ -85,0 +119,0 @@ }

{
"author": "Nate Hunzaker <nate.hunzaker@gmail.com> (natehunzaker.com)",
"name": "backbone-server",
"description": "An interface between Backbone and Express. Creates a Backbone.Server object.",
"version": "0.2.2",
"description": "Creates a Backbone.Server object which interfaces between Backbone, Socket.IO and Express.",
"version": "0.3.0",
"main": "./lib/backbone-server.js",

@@ -17,4 +17,7 @@ "repository": {

"dependencies": {
"express": ">= 2.4.6",
"backbone": ">= 0.5.3"
"underscore" : "*",
"backbone" : "*",
"express" : "*",
"socket.io" : "*",
"ejs" : "*"
},

@@ -21,0 +24,0 @@

@@ -18,2 +18,3 @@ # Backbone-Server

'routes' : {}, // Any initial routes you may have (or create them all at once)
'socketio' : false // If set to true, Backbone.Server will enable socket.io functionality
});

@@ -30,3 +31,3 @@ ```

});
``
```

@@ -60,2 +61,64 @@ ## Routes

```
## Using Socket.io functions:
To enable socket.io functionality, set the `socketio` attribute to `true` upon creation of your new Backbone.Server object:
```
var server = new Backbone.Server(
'socketio' : true // If set to true, Backbone.Server will enable socket.io functionality
});
```
This will create an instance of socket.io at the `io` attribute of your new server object (`server.io`). You can now add standard socket.io events using the `send` and `receive` methods, like so:
```
// Socket.IO Functions
server.receive('connection', function() {
console.log("connected");
//... enter your server side code to create your models
server.send('models', [a, b, c, d]);
});
```
In your clientside application, you can receive commands from the server like so (note that backbone-server automatically
provides a route to load Underscore and Backbone):
```
<script src="/backbone-server/backbone.js" type="text/javascript"></script>
<script src="/socket.io/socket.io.js" type="text/javascript"></script>
<script>
var socket = io.connect('http://localhost');
var Article = Backbone.Model.extend();
var Articles = Backbone.Collection.extend({
model: Article,
initialize: function() {
// Bind an event to notify when an article has been added to the server
this.bind('add', function(article) {
console.log("Article added!");
});
}
});
var articles = new Articles();
socket.on('models', function(data) {
_.each(data, function(d) {
articles.add(d); // queues the collection event ('articles added!');
});
});
</script>
```
To summarize, in the example above, we've created a `Backbone.Server` which will push down server side models upon connection, and add them
to the client scripts `Articles` collection.
## Starting the Server:

@@ -67,6 +130,16 @@

## Don't see something here you like from Express?
## Don't see something here you like from Express or Socket.io?
Target Express directly:
###Target Express directly:
`server.app // === Express `
```
// server.express == Express
server.express
```
###Target Socket.io directly:
```
// server.io == socket.io
server.io
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc