Permalink
Browse files

[slideshow, slide, server] move broadcasting to slideshow module, add…

… 'pre' save hook for slide and remov broadcasting from module
  • Loading branch information...
indutny committed Jul 1, 2011
1 parent 59bad4a commit 2e8b2c6a63d309abd64d1bf85353c0070ba5203d
Showing with 58 additions and 34 deletions.
  1. +34 −0 lib/slides/server.js
  2. +6 −29 lib/slides/slide.js
  3. +18 −0 lib/slides/slideshow.js
  4. +0 −5 public/js/main.js
View
@@ -136,6 +136,8 @@ Server.prototype.listen = function() {
* create models, etc
*/
Server.prototype.initDb = function() {
+ var that = this
+
// Create Schema&Models
var models = this.models = {
User: new Schema({}),
@@ -176,8 +178,38 @@ Server.prototype.initDb = function() {
index: 1
}, {unique: true});
+ // Post callback
+ models.Slide.pre('save', function(next, callback, slideshow) {
+ var slide = this,
+ slideshowId = slide.get('slideshow');
+
+ slideshow ? withSlideshow(slideshow) : withoutSlideshow();
+
+ function withoutslideshow() {
+ Slideshow.findById(slideshowId, function(err, slideshow) {
+ if (err) return next(err);
+ if (!slideshow) {
+ return next(new Error('Slideshow ' + id + ' not found'));
+ }
+
+ withSlideshow(slideshow);
+ });
+ };
+
+ function withSlideshow(slideshow) {
+ slides.slideshow.broadcast(that.redis, slideshow, 'update', {
+ index: slide.get('index'),
+ content: slide.get('content'),
+ contentType: slide.get('contentType')
+ });
+ next();
+ };
+ });
+
// Setup auth
var User,
+ Slideshow,
+ Slide,
authOptions = utils.merge({
everymodule: {
everyauth: {
@@ -197,6 +229,8 @@ Server.prototype.initDb = function() {
// Set reference
User = mongoose.model('User');
+ Slideshow = mongoose.model('Slideshow');
+ Slide = mongoose.model('Slide');
// Connect to Database
mongoose.connect(this.options.db.mongodb);
View
@@ -37,18 +37,6 @@ slide.routes = function(app, redis) {
var Slideshow = mongoose.model('Slideshow'),
Slide = mongoose.model('Slide');
- function broadcast(slideshow, action, data) {
- event = JSON.stringify({
- action: action,
- data: data
- });
- redis.publish('slideshow.' + slideshow._id, event);
-
- if (slideshow.slug.toString() != slideshow._id.toString()) {
- redis.publish('slideshow.' + slideshow.slug, event);
- }
- };
-
// List
app.get('/api/slideshow/:slideshow/slide', function(req, res) {
slideshow.get(req, res, function(slideshow) {
@@ -59,7 +47,8 @@ slide.routes = function(app, redis) {
if (err) return res.jsonError(err);
if (!slides) return res.json(404);
- redis.get('slideshow.' + slideshow._id + '.current', function(err, num) {
+ var key = 'slideshow.' + slideshow._id + '.current';
+ redis.get(key, function(err, num) {
if (err) return res.jsonError(err);
res.json({
@@ -112,7 +101,7 @@ slide.routes = function(app, redis) {
}
function next(index) {
- broadcast(slideshow, 'select', +index);
+ slides.slideshow.broadcast(redis, slideshow, 'select', +index);
}
});
});
@@ -136,13 +125,7 @@ slide.routes = function(app, redis) {
slide.save(function(err, slide) {
if (err) return res.jsonError(err);
res.json(200);
-
- broadcast(slideshow, 'create', {
- index: slide.index,
- content: slide.content,
- contentType: slide.contentType
- });
- });
+ }, slideshow);
});
});
@@ -171,12 +154,7 @@ slide.routes = function(app, redis) {
slide.save(function(err, slide) {
if (err) return res.jsonError(err);
res.json(200);
- broadcast(slideshow, 'update', {
- index: slide.index,
- content: slide.content,
- contentType: slide.contentType
- });
- });
+ }, slideshow);
});
});
@@ -190,8 +168,7 @@ slide.routes = function(app, redis) {
slide.remove(function(err) {
if (err) return res.jsonError(err);
res.json(200);
- broadcast(slideshow, 'delete', slide.index);
- });
+ }, slideshow);
});
});
};
View
@@ -48,6 +48,24 @@ slideshow.get = function(req, res, callback) {
};
};
+/**
+ * Redis broadcasting
+ */
+slideshow.broadcast = function(redis, slideshow, action, data) {
+ data = JSON.stringify({
+ action: action,
+ data: data
+ });
+
+ var id = slideshow._id.toString();
+
+ redis.publish('slideshow.' + id, data);
+
+ if (id != slideshow.slug.toString()) {
+ redis.publish('slideshow.' + slideshow.slug, data);
+ }
+};
+
/**
* Slideshow CRUD
*/
View
@@ -87,11 +87,6 @@
}, -1);
};
- socket.on('create', function(slide) {
- slides.push(renderSlide(slide));
- rerender();
- });
-
socket.on('update', function(slide) {
var index = findSlide(slide.index);

0 comments on commit 2e8b2c6

Please sign in to comment.