Checking for latest revision when returning a 404

This commit is contained in:
Jon Linklater-Johnson 2012-08-10 15:34:58 +01:00 committed by Aron Carroll
parent 7e8d1099cb
commit b1faabb9d5

View File

@ -351,23 +351,41 @@ module.exports = Observable.extend({
}
results.url = req.param('bin');
if (req.url.indexOf('edit') > -1) {
_this.render(req, res, results);
} else {
var options = {edit: true, silent: true};
_this.formatPreview(results, options, function (err, formatted) {
if (err) {
next(err);
}
if (formatted) {
res.send(formatted);
} else {
res.contentType('js');
res.send(req.bin.javascript);
}
});
}
// Need to get the most recent revision from the database.
_this.models.bin.latest({id: results.url}, function (err, bin) {
if (err) {
return next(err);
}
// We have a missing bin, we check the latest returned bin, if this
// is active then we simply render it assuming the user mistyped the
// url.
if (bin && bin.active) {
results = bin;
} else {
// If we have a bin then take the latest revision plus one.
results.revision = bin && (bin.revision + 1);
}
if (req.url.indexOf('edit') > -1) {
_this.render(req, res, results);
} else {
var options = {edit: true, silent: true};
_this.formatPreview(results, options, function (err, formatted) {
if (err) {
next(err);
}
if (formatted) {
res.send(formatted);
} else {
res.contentType('js');
res.send(req.bin.javascript);
}
});
}
});
});
},
loadBin: function (req, res, next) {