# Backbone-Server

Backbone-Server creates a Server module which pulls together Backbone and Express to bring greater structure to your
NodeJS applications.

Backbone.Server can be accessed just like any other Backbone module:

```
var Backbone = require('backbone-server'); // Note: Backbone-Server relies on the backbone npm package and is not required.

// Current defaults for Backbone.Server
var server = new Backbone.Server({
    'port'         : 8000,
    'public'       : 'public',      // The Public Director
    'views'        : 'views',       // The Views Director
    'view-engine'  : 'jade',        // Desired Templating Engine
    '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
});
```

And of course, you can always extend it in traditional Backbone form:

```
var Blog = Backbone.Server.extend({

    // Custom features can go here

});
```

## Routes

Once you have added a Server, add routes just as you would with Express:

```
server.get('/post/:id', function(req, res) {
    var id = req.params.id,
        article = articles.get(id);
    
    res.render('single', article);
});

server.post('/post', function(req, res) {
    articles.add(req.params);
});

server.put('/post/:id', function(req, res) {
    var article = article.get(req.params.id);
    
    article.set(req.params);
});

server.delete('/post/:id', function(req, res) {
    articles.remove(params.req.id);    
});

```

## 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:

Once you have created a server starting it is easy:

`server.start();`

## Don't see something here you like from Express or Socket.io?

###Target Express directly:

```
// server.express == Express
server.express
```

###Target Socket.io directly:

```
// server.io == socket.io
server.io
```