使用服务器/客户端文件夹时,流星反应性中断

时间:2014-06-04 13:34:24

标签: meteor

所以我做了一些超级简单的东西来测试Meteor中的反应性但是当我来制作服务器和客户端文件夹时反应性破裂了。我无法再手动编辑数据库并立即在浏览器中查看更改。

模板:

<template name="hello">
  <input type="button" value="Click" />
    {{#each tt}}
        {{test}}
    {{/each}}
</template>

的客户机/ test.js:

Template.hello.events(
{
    'click input': function ()
    {
        Meteor.call('set');
    }
});

Template.hello.helpers(
{
    tt: function()
    {
        Meteor.call('get', function(error, result)
        {
            Session.set('aa', result);
        });


        return Session.get('aa');

    }
});

服务器/ testS.js:

Test = new Meteor.Collection("test");

Meteor.methods(
{
    set: function()
    {
        Test.insert({test: "test 1"});
    },
    get: function()
    {
        return Test.find().fetch();
    }
});

使用此文件夹结构获取反应性时我缺少什么?

1 个答案:

答案 0 :(得分:2)

以下是一个问题。

Meteor.call('get', function(error, result) {
    Session.set('aa', result);
});

在您的情况下,这只会发生一次Meteor.call通常表示为单一请求,与发布/订阅模型完全不同。唯一的&#34;反应性&#34;如果您手动执行Session.set('aa', result);

,您将遇到这种情况

如果您想要客户端/服务器数据库之间的反应,则需要设置发布/订阅代码(请参阅http://docs.meteor.com/#meteor_publish)。默认情况下,数据库中的所有文档都会通过自动发布包发布到客户端,因此请记住这一点。这是为了自动允许你在客户端上执行Collection.find()之类的东西,它返回一个游标,默认情况下是被动的。

换句话说,您的Meteor.call是多余的。 Test集合已存在于客户端上,允许您执行以下操作。

Template.hello.helpers({
    tt: function() {
        return Test.find();
    }
});