我是Meteor的新手,开始编写一个连接到非基于Web的服务器的流星应用程序。由于所有数据都应通过此服务器进行路由,因此它具有用于获取和操作相关对象的REST接口。
现在我被困在如何为这个Web服务构建一个集合。我尝试使用此处显示的方法:https://medium.com/meteor-js/how-to-connect-meteor-js-to-an-external-api-93c0d856433b
这适用于获取当前结果。但是当我添加/删除/更新记录时,根本没有更新。
在服务器端,我发布的内容如下:
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);
}
我在added()
中读到了有关Meteor.publish()
函数和类似函数的内容,但无法理解我是如何使用它来启用服务器和客户端或集合之间的“即时”同步和ui elements。
所以基本上我想知道如何建立一个不是基于数据库而是基于REST接口的反应集合。
有人可以给我一些关于如何实现这一目标的建议/提示吗?
答案 0 :(得分:1)
出版物不会自动重新运行HTTP请求。您需要以某种方式检测API中的数据何时发生更改(简单的方法是在Meteor.setInterval
上运行查询)并调用this.updated
和this.removed
以通知客户端变化。
答案 1 :(得分:0)
我不确定,但我猜你订阅的plantaTasks
从未发布(并且你在服务器端发布了someThings
。)