0

我尝试用流星构建一个家庭自动化系统。因此我想做以下事情。

我有一个集合,其中包含我从任何来源读取的所有不同的 liveValues。每个文档都是具有实际值的例如传感器的值。

现在我想创建第二个名为 thing 的集合。在这个集合中,我想添加我所有的“事物”,例如“室温生活”和这个事物的数据。一个属性应该是与 liveValues 之一的连接。

现在我想使用 Meteor 发布和订阅 Thing 集合,因为在 Web 界面上,Thing 背后的 liveValue 无关紧要。

在这里,在我的选项中,复杂的部分开始了。

我如何将数据发布到客户端,并且当 LiveValue 更改为该事物时,我将进行反应性更新?因为它是一个不同于“Thing”集合的集合。

在我的想法中,我想通过一个订阅一个“事物”文档来做到这一点,我将通过这个订阅返回 liveValue 集合的 liveValue 更新。

这可行吗?

有人知道我该如何处理吗?

我听说过有关流星反应发布的一些信息,但我不确定这是否是解决方案。我也听说这需要大量的服务器电源。

谢谢你的帮助。

4

1 回答 1

0

所以基本上你想将服务器端的文档合并到客户端的一个反应式集合中。

您应该使用observeChanges由 Meteor Collections 提供的文档,如文档中所述。

通过这种方式,您可以观察服务器端集合的变化并发布到客户端聚合集合,如下所示:

// Get the data from a kind of sensor
var cursor = SomeSensor.find({/* your query */});

var self = this;

// Observe the changes in the cursor and publish
// it to the 'things' collection in client
var observer = cursor.observeChanges({
  added: function (document) {
    self.added('things', document._id, document);
  },
  removed: function (document) {
    self.removed('things', document._id, document);
  },
  changed: function (document) {
    self.changed('things', document._id, document);
  }
});

// Make the publication ready
self.ready();

// Stop the observer on subscription stop
self.onStop(function () {
  observer.stop();
});

有了这个,things集合将反应性地拥有来自所有传感器的数据。

希望它可以帮助你。

于 2016-11-13T14:44:25.797 回答