我正在开发一个应用程序,其中有一个可搜索的参考数据库,并且有一个允许用户编辑数据库的管理应用程序。面向公众的一面使用 Knockout JS 通过 RESTful API 与服务器通信。管理应用程序使用 express。页面是使用 Handlebars 从模板呈现的。编辑视图使用与公共视图相同的呈现代码。
我正在使用 connect-flash 向编辑器发送操作结果消息。当表单提交到页面(用于添加新引用)时,会按预期显示闪烁消息,但是,对于表格视图中每个项目上可用的命令(如删除特定引用),我使用来自视图模型中定义的函数的 ajax 调用。调用正确执行并执行了操作,但永远不会向用户显示 Flash 消息。我有一个自定义中间件,它将 Flash 消息收集到模板的替换对象中。当我记录发送了哪些消息时,我从来没有看到删除调用产生的消息,所以我不仅仅是在额外刷新时丢失了它。
这是我正在使用的请求处理程序:
var router = express.Router();
var client = solr.createClient(...);
router.get('/', function (req, res, next) {
res.render('refs', req.replacements);
});
// This one is submitted via an html form post, and works
router.post('/new', function (req, res, next) {
var doc = {};
... // Form reading and validation code
client.add(doc, { commitWithin: 50 }, function (err, data) {
if (err) {
console.log(err);
req.flash('error', 'A problem occurred during submit.'); //works
} else { // Success
req.flash('yay', 'New reference successfully added.'); //works
}
res.redirect(303, '/refs?rows=1&q=id%3A' + newId);
});
});
// This one is submitted via ajax in the view model. Action works, flash doesn't
router.delete("/:id(\\d+)", function (req, res, next) {
var id = req.params.id;
var query = 'q=id:' + id;
var doc = undefined;
client.get('refs', query, function (err, obj) {
if (err) {
req.flash('error', 'Unable to obtain a copy of object to delete for audit log. Reference not deleted.');
res.redirect(303, '/refs');
} else {
doc = obj.response.docs[0];
client.deleteByID(id, { commitWithin: 50 }, function (err, data) {
if (err) {
console.log(err);
return req.flash('error', 'A problem occurred during delete submission.');
} else { // Success
req.flash('yay', 'Reference successfully deleted.'); // Never seen
}
});
}
});
res.redirect(303, url.format({
pathname:"/refs",
query:req.query,
}));
});
我已经尝试将重定向更改为 278 以防止浏览器透明地处理重定向,然后在 ajax 调用的成功函数中处理它,按照How to manage a redirect request after a jQuery Ajax call,但这没有帮助.
这是定义视图模型的客户端javascript:
function RefsViewModel(qString) {
"use strict";
var self = this;
self.refs = ko.observableArray();
$.ajax({
// This makes the call which populates self.refs with observables
});
self.deleteRef = function (ref) {
$.ajax({ // Makes an AJAX query to the server for the source
url: "/refs/" + ref.id(),
type: "DELETE",
error: function (jqXHR) {
console.log("ajax error " + jqXHR.status);
alert("Error sending delete request");
}
});
}
}
我用来收集闪存消息的中间件:
var flash = require('connect-flash');
function flashMessageCenter(req, res, next) {
if(typeof req.replacements === "undefined") {
req.replacements = {}
}
req.replacements.errorMessage = req.flash("error");
req.replacements.yayMessage = req.flash("yay");
req.replacements.infoMessage = req.flash("info");
console.log(req.replacements);
return next();
}