【问题标题】:meteor reactivity breaks when using server/client folders使用服务器/客户端文件夹时流星反应中断
【发布时间】:2014-06-04 23:12:06
【问题描述】:

所以我做了一些超级简单的东西来测试 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();
    }
});

使用此文件夹结构获得响应性我缺少什么?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    以下是一个问题。

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

    这仅在您的情况下发生一次Meteor.call 通常表示单个请求,与发布/订阅模型完全不同。在这种情况下,您会遇到的唯一“反应”是如果您手动执行 Session.set('aa', result);

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      相关资源
      最近更新 更多