流星反应式发布(Meteor reactive publish)

编程入门 行业动态 更新时间:2024-10-28 20:17:11
流星反应式发布(Meteor reactive publish)

此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'; } });

更多推荐

本文发布于:2023-04-29 05:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反应式   流星   Meteor   reactive   publish

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!