简体   繁体   English

实时更改内容-流星

[英]Change content Real-time - meteor

I have small app (My first Meteor App), where I want add changing real-time content. 我有一个小型应用程序(我的第一个Meteor应用程序),我想在其中添加不断变化的实时内容。 So, my code looks like this 所以,我的代码看起来像这样

collection 采集

Content = new Meteor.Collection('content');

client 客户

if (Meteor.isClient) {

  Meteor.subscribe('content');

  Template.contentTpl.events({
    'keyup #content': function (e) {
      if (!Content.find({_conId: / some id from session / }).count()) {
        Content.insert({text: e.target.value, _conId: / some id from session / });
      } else {
        Content.update({_conId: / some id from session / }, {text: e.target.value});
      } 
    }
  });

  Template.contentTpl.content = function () {
    return Content.findOne({});
  };
}

<template name="contentTpl">
  <textarea id="content" rows="10" cols="100">{{content.text}}</textarea>
</template>

and server 和服务器

if (Meteor.isServer) {
  Meteor.publish('content', function () {
    return Content.find();
  });
}

But when I open this app in two different browser I see the following, when I'm typing in the first browser - content changing in another browser but when I'm typing in another browser in first browser nothing happens. 但是,当我在两个不同的浏览器中打开此应用程序时,看到以下内容,当我在第一个浏览器中键入内容时-在另一个浏览器中更改内容,但是当我在第一个浏览器中键入另一个浏览器时却什么也没有发生。 How I can add changing realtime in meteor ? 如何添加流星实时变化? Should I use somethin like socket.io ? 我应该使用socket.io之类的东西吗?

ps In docs I found observe method, but I don't quite understand how to use it to change the property and not for all collection. ps在文档中,我找到了Observ方法,但是我不太了解如何使用它来更改属性,而不是用于所有集合。

Thx. 谢谢。

From the Meteor documentation : 从流星文档中

By default, new Meteor apps automatically include the preserve-inputs package. 默认情况下,新的Meteor应用程序会自动包含keep-inputs包。 This preserves all elements of type input, textarea, button, select, and option that have unique id attributes or that have name attributes that are unique within an enclosing element with an id attribute. 这将保留类型输入,文本区域,按钮,选择和选项的所有元素,这些元素具有唯一的id属性或具有带有id属性的封闭元素中唯一的名称属性。 To turn off this default behavior, simply remove the preserve-inputs package. 要关闭此默认行为,只需删除preserve-inputs包。

So once you typed in the textarea in the first browser the text won't change even though content.text changes as triggered by the change in the other browser. 因此,一旦您在第一个浏览器中输入了textarea,即使content.text由其他浏览器的更改触发而更改,该文本也不会更改。

You may observe that reactivity is correctly working by putting the handlebars expression {{content.text}} outside the textarea, like so: 您可能会发现,通过将把手表达式{{content.text}}放在文本区域之外,可以正确地进行反应,如下所示:

<template name="contentTpl">
  <textarea id="content" rows="10" cols="100">{{content.text}}</textarea>
  <p>
  {{content.text}}
</template>

To update the text in the textarea you can remove the preserve-inputs package by executing: 要更新textarea中的文本,您可以通过执行以下操作来删除preserve-inputs包:

meteor remove preserve-inputs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM