如何在流星中构建反应式 Web 服务支持的集合

how to build a reactive web service backed collection in meteor

我对 Meteor 很陌生,开始编写一个连接到非基于 Web 的服务器的 Meteor 应用程序。由于所有数据都应通过此服务器路由,因此它具有用于获取和操作相关对象的 REST 接口。

现在我被困在如何为这个 Web 服务创建一个集合上ui。我尝试使用此处显示的方法:https://medium.com/meteor-js/how-to-connect-meteor-js-to-an-external-api-93c0d856433b

这适用于获取当前结果。但是我add/delete/update记录的时候,根本就没有更新

在服务器端,我是这样发布的:

Meteor.publish('someThings', function() {
    var self = this;

    try {
        var records = HTTP.get("http://localhost:6789/things/", {auth: "user:passwd"});

        _.each(records.data, function(record) {

            var thing = {
                uuid: record.uuid,
                title: record.title,
            };

            self.added('things', thing.uuid, thing);
        });

        self.ready();
    } catch (error) {
        console.log(error);
    }
  }
);

然后在全球范围内,我有一个集合:

SomeThings = new Meteor.Collection("things");

我在 React 组件中这样使用它:

SomeThings = new Meteor.Collection("things");

getMeteorData() {
    return {
      things: SomeThings.find({}).fetch()
    };
  },

在客户端的某个地方,我另外添加了这个(如在 howto 中):

  Tracker.autorun(function() {
      Meteor.subscribe('someThings');
  });

最后在服务器端我有一些函数可以进行操作,一次通过 REST 接口,一次在集合上(例如:插入):

addThing: function(title) {
  result = Meteor.http.post("http://localhost:6789/things/",
    {auth: "user:passwd", params: {title:title}});
  SomeThings.insert(result.data);
}

我在 Meteor.publish() 中阅读了一些关于 added() 函数和类似函数的内容,但无法理解 how/if 我可以使用它来启用 "instant" 服务器和服务器之间的同步客户端或集合和 ui 元素。

所以基本上我想知道如何构建uild 一个不基于数据库而是基于 REST 接口的反应式集合。

有人可以 advice/hints 告诉我如何实现吗?

我不确定,但我猜你订阅了从未发布过的 plantaTasks(你在服务器端发布了 someThings)。

发布不会自动重新运行 HTTP 请求。您将需要以某种方式检测 API 中的数据何时更改(简单的方法是 运行 Meteor.setInterval 上的查询)并调用 this.updatedthis.removed 通知客户更改。