此Meteor代码在模板上的headerLabel上显示消息,服务器和/或客户端通过在HeaderLabelCol mongo集合中插入新消息来更改消息,并期望客户端模板发布,因为它发布了最后插入的文档。
我能够使用客户端浏览器插入新消息,但直到我刷新页面才显示,这可能表明反应链在某处被破坏。 问题是什么? 怎么修好? 谢谢
//client.js Template.header.helpers({ headerLabel: function () { return HeaderLabelCol.findOne() ? HeaderLabelCol.findOne().headerLabel : 'Make a selection'; } }); //server.js HeaderLabelCol = new Mongo.Collection('headerLabelCol'); Meteor.publish('headerLabelCol', function () { return HeaderLabelCol.find({userId: this.userId}, { sort: { createdAt: -1 } }); }); HeaderLabelCol._ensureIndex({createdAt: -1}); HeaderLabelCol.before.insert(function (userId, doc) { doc.userId = userId; doc.createdAt = Date.now(); }); HeaderLabelCol.allow({ insert: function (userId, doc) { return (userId && doc.owner === userId); } });This Meteor code displays a message on a headerLabel on a template, the server and/or the client changes the message by inserting a new message in HeaderLabelCol mongo collection and expect the client template to change since it publishes the last inserted document.
I was able to insert a new message using the client browser but did not show till I refreshed the page which may indicate that the reactiveness chain is broken somewhere. What is the problem? How can it be fixed? Thanks
//client.js Template.header.helpers({ headerLabel: function () { return HeaderLabelCol.findOne() ? HeaderLabelCol.findOne().headerLabel : 'Make a selection'; } }); //server.js HeaderLabelCol = new Mongo.Collection('headerLabelCol'); Meteor.publish('headerLabelCol', function () { return HeaderLabelCol.find({userId: this.userId}, { sort: { createdAt: -1 } }); }); HeaderLabelCol._ensureIndex({createdAt: -1}); HeaderLabelCol.before.insert(function (userId, doc) { doc.userId = userId; doc.createdAt = Date.now(); }); HeaderLabelCol.allow({ insert: function (userId, doc) { return (userId && doc.owner === userId); } });最满意答案
我认为您还需要在助手中添加条件。
//client.js Template.header.helpers({ headerLabel: function () { var result = HeaderLabelCol.findOne({}, { sort: { createdAt: -1 } }); return result ? result.headerLabel : 'Make a selection'; } });I think you need to add the condition in your helper as well.
//client.js Template.header.helpers({ headerLabel: function () { var result = HeaderLabelCol.findOne({}, { sort: { createdAt: -1 } }); return result ? result.headerLabel : 'Make a selection'; } });更多推荐
发布评论