1

我在 Meteor 中订阅/发布时遇到问题。我编写了一个示例 Meteor 应用程序来帮助缩小问题的范围。

我正在服务器上发布一个集合,该集合由通过客户端订阅传递的参数过滤。此订阅在自动订阅中,它利用会话变量来响应式更新订阅。

在更改此特定会话变量的状态时,客户端上的集合没有得到正确更新,或者至少我收集到的是这样。我花了一整天的时间在我控制的代码中没有发现问题。我怀疑我要么不了解如何在 Meteor 中设置正确的 pub-sub,要么 Meteor 中存在问题。

要重现该问题,请启动一个新的 Meteor 项目并使用以下内容(尝试时确保删除自动发布包):

HTML(例如 test.html):

<head>
    <title>pubsubbug</title>
</head>

<body>
    {{> main}}
</body>

<template name="main">
    <h1>Example showing possible bug in Meteor wrt pub-sub</h1>
    <p><button name="showall">show all ({{showall}})</button></p>
    <div style="float:left;width:400px;">
        <h2>Notes:</h2>
        <ul>
            {{#each notes}}
                <li>{{title}}</li>
            {{/each}}
        </ul>
    </div>
    <div style="float:left;">
        <h2>Notes (copied):</h2>
        <ul>
            {{#each notes_copied}}
                <li>{{title}}</li>
            {{/each}}
        </ul>
    </div>
</template>

JS(例如 test.js)

if (Meteor.is_client) {
    Notes = new Meteor.Collection("notes_collection");
    NotesCopied = new Meteor.Collection("notes_collection_copied");

    Session.set("showall", false);

    Meteor.autosubscribe(function () {
        Meteor.subscribe("notes_subscription", Session.get("showall"), function () {
            console.log("Notes count:", Notes.find().count());
        });

        Meteor.subscribe("notes_subscription_copied", Session.get("showall"), function () {
            console.log("Bug? This isn't getting called.");
            console.log("NotesCopied count:", NotesCopied.find().count());
        });
    });

    Template.main.notes = function () {
        return Notes.find();
    };

    Template.main.notes_copied = function () {
        return NotesCopied.find();
    };

    Template.main.showall = function () {
        return Session.get("showall");
    };

    Template.main.events = {
        "click button[name='showall']": function (evt) {
            Session.set("showall", !Session.get("showall"));
        }
    };
}

if (Meteor.is_server) {
    Notes = new Meteor.Collection("notes_collection");

    var getNotes = function (showall) {
        if (showall) {
            return Notes.find({}, {sort: {title: 1}});
        } else {
            return Notes.find({visible: true}, {sort: {title: 1}});
        }
    };

    Meteor.publish("notes_subscription", function (showall) {
        // By sending the Notes back with the same uuid as before, the
        // client end seems to get confused:
        return getNotes(showall);
    });

    Meteor.publish("notes_subscription_copied", function (showall) {
        var notes = getNotes(showall);
        var self = this;

        // Copy notes into a new notes collection (see NotesCopied on client).
        // By generating a new uuid, we don't get an issue with the notes
        // on the client getting screwed up:
        notes.forEach(function (note) {
            var uuid = Meteor.uuid();   // note._id will cause same problem
            self.set("notes_collection_copied", uuid, {title: note.title});
        });

        self.flush();
        self.complete();
    });

    // Add example notes
    Meteor.startup(function () {
        if (Notes.find().count() === 0) {
            Notes.insert({title: "Note #1 (always visible)", visible: true});
            Notes.insert({title: "Note #2 (always visible)", visible: true});
            Notes.insert({title: "Note #3 (always visible)", visible: true});
            Notes.insert({title: "Note #4 (only visible when showall true)", visible: false});
            Notes.insert({title: "Note #5 (only visible when showall true)", visible: false});
            Notes.insert({title: "Note #6 (only visible when showall true)", visible: false});
        }
    });
}

您将看到的内容的解释:

将有一个按钮,单击该按钮时,只需在真假之间切换会话变量(showall)。

存在两个订阅(在自动订阅中),一个示例错误,另一个以_copied. 我不知道如何处理这些特定的信息......我不想要新的 uuid。

所以基本上,当重复单击显示全部按钮时,第一列注释:将显示不正确的结果,单击几下后不会显示任何内容。

另一方面,第二列Notes (copied):,其 uuid 每次都重新生成,正确显示。

这是一个错误吗?或者有没有合适的方法来做到这一点?

编辑:上面的例子在http://pubsubbug.meteor.com/

4

1 回答 1

2

在 Windows 上的开发人员分支上没有遇到您的错误。既然是这种情况,这是一个好兆头,表明您的代码没有任何问题。您似乎看到有关订阅和/或 Mongo 查询方式的问题。

Meteor 本身很可能在其主机上运行稳定(= master)版本,因此您将不得不尝试不同的方法或等待新版本。除非你可以支持在开发中运行...

于 2012-05-15T23:07:09.083 回答