scttnlsn / backbone.io

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

Examples of sessionStore and mongooseStore #7

Closed jackp closed 12 years ago

jackp commented 12 years ago

Hi Scott,

Awesome library. I'm trying to get it all working in my project but am having some trouble getting the mongooseStore and sessionStore middleware working. Could you provide some examples that use them?

I'm trying to do two things:

  1. I have a User model which I will want to sync with my backend's sessionStore.
  2. All others models will sync directly with mongoose

Many thanks

lboynton commented 11 years ago

Any reason this is closed? I'm looking for the same thing.

scttnlsn commented 11 years ago

Check out some of the existing examples. You can really just replace the memory store with the Mongoose store (pass the mongooseStore function a Mongoose model). Something like:

var users = backboneio.createBackend();
users.use(backboneio.middleware.cookieParser());
users.use(backboneio.middleware.session({ store: sessions }));
users.use(backboneio.middleware.mongooseStore(User));
lboynton commented 11 years ago

Thanks, I've managed to work it out. However, deleting a model doesn't seem to be working. I'm using this for the server side and the same version of the index.html in examples.

var express = require('express');
var backboneio = require('../lib/index');
var mongoose = require('mongoose');
mongoose.connect('localhost', 'test');

var app = express.createServer();
app.use(express.static(__dirname));

app.listen(3000);
console.log('http://localhost:3000/');

var schema = new mongoose.Schema({
    text: String
});

var Message = mongoose.model('message', schema);

var messages = backboneio.createBackend();
messages.use(backboneio.middleware.mongooseStore(Message));

backboneio.listen(app, { messages: messages });
scttnlsn commented 11 years ago

If you're using MongoDB, make sure you have idAttribute: '_id' set in your client-side Backbone model.

lboynton commented 11 years ago

Bingo. Thanks.