scttnlsn / backbone.io

Backbone.js sync via Socket.IO
http://scttnlsn.github.io/backbone.io
541 stars 66 forks source link

Syncing multiple backbone models with the server #10

Closed dombesz closed 11 years ago

dombesz commented 12 years ago

Hi,

I'm trying to set up backbone.io with multiple models. I saw that the request contains a model attribute. But if I'm syncing a model with backbone.io the req.model will be [] . If I'm trying to sync collections with the server the req.model contains all the objects that I have in the collection. What I'm trying to achieve is to handle the crud actions on the server side depending on the model type. So for example if I have a posts and a comments model I'd like to handle them in separate middlewares on the serverside. Something like:

var backend = backboneio.createBackend();

backend.use(function(req, res, next) {
   if(req.model=='post'){
     //handle post crud
   }else{
     //handle comments crud
   }
});

Another option I see is to have separate backends for each model and bind the appropiate backend to each model/collection on the backbone frontend.

var posts = backboneio.createBackend();
var comments = backboneio.createBackend();

posts.create(function(req, res, next){
  //handle create...
}

comments.create(function(req, res, next){
  //handle create...
}

Can you point me to the right direction please? Thanks.

scttnlsn commented 12 years ago

I would usually recommend creating a separate backend for each "content type". Posts and comments, however, may be an exception since comments usually belong to a particular post. You could nest the comments inside the post model and handle saving both in a single backend. The "right" approach to take will likely depend on how you're storing the data, etc. Does this answer your question?

dombesz commented 12 years ago

Sorry, my explanation wasn't the clearest. So my problem is that the req.model attribute is not for what i'm looking for, its either empty [] on the case of model binding or contains an array of objects in case of collection binding. So on the server side cant really rely on the model attribute. And my question is that, how the req.model attribute supposed to work?

scttnlsn commented 11 years ago

The req.model will contain the data that was sent to Backbone.sync. The middleware layers can do whatever they like with it, save it to disk, modify it, etc.