Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 渲染使用Meteor动态创建的新div_Node.js_Meteor - Fatal编程技术网

Node.js 渲染使用Meteor动态创建的新div

Node.js 渲染使用Meteor动态创建的新div,node.js,meteor,Node.js,Meteor,我有一个经典的“线程->帖子”模型。 在应用程序中,我有一个包含“线程”集合的左侧边栏。当用户点击一个线程时,我想用Thread->Posts元素创建另一个div 有什么方法可以做到这一点,保护反应性吗 现在,我有这个: // In the client Template.threadlist.events({ 'click tr': function(event){ Session.set("selectedThread",this);

我有一个经典的“线程->帖子”模型。 在应用程序中,我有一个包含“线程”集合的左侧边栏。当用户点击一个线程时,我想用Thread->Posts元素创建另一个div

有什么方法可以做到这一点,保护反应性吗

现在,我有这个:

// In the client
   Template.threadlist.events({
        'click tr': function(event){
            Session.set("selectedThread",this);
            $("#posts").html( Meteor.render(Template["datathread"]) );
        }
   })

    [...]


    Template.datathread.events({
        'click input.add-post' : function(event){
            var t = Session.get("selectedThread");
            Meteor.call("addPost", {"thread":t,"text":"foo","user":"var"},callback})
        }
    })
   [...] 
   // In the server

   addPost: function(param){
    var id = Threads.update(param.thread,{ $addToSet: {"posts": {"text":param.text, "user": param.user}}});
    return id;
 }
包含帖子的模板如下所示:

<template name="datathread">
{{#each thread.posts}}
    {{user}} says: {{text}}
    <br />
{{/each}}
</template>
// In the client
Template.datathread.thread = function(){
    return Session.get("selectedThread");
}

[...]

Template.threadlist.events({
    'click tr': function(event){
        Session.set("selectedThread",this);
     }
});


//In the html  
<div class="span5" id="threads">
    {{> datathread}}
</div>

<template name="datathread">
    {{#if thread}}
        <hr />
        {{#each thread.posts}}
            {{user}} says: {{text}}
            <br />
        {{/each}}
    {{/if}}
</template>

{{{#每个线程.posts}
{{user}}说:{{text}

{{/每个}}
(“user”和“text”属性来自“thread.posts”元素)

使用这段代码,我只得到刷新(F5)网页(或执行'click tr'事件)的新值。我做错了什么

谢谢大家!

==编辑==

好的。。。现在,在Chistian Fritz的推荐下,我的代码是这样的:

<template name="datathread">
{{#each thread.posts}}
    {{user}} says: {{text}}
    <br />
{{/each}}
</template>
// In the client
Template.datathread.thread = function(){
    return Session.get("selectedThread");
}

[...]

Template.threadlist.events({
    'click tr': function(event){
        Session.set("selectedThread",this);
     }
});


//In the html  
<div class="span5" id="threads">
    {{> datathread}}
</div>

<template name="datathread">
    {{#if thread}}
        <hr />
        {{#each thread.posts}}
            {{user}} says: {{text}}
            <br />
        {{/each}}
    {{/if}}
</template>
//在客户端中
Template.datathread.thread=函数(){
return Session.get(“selectedThread”);
}
[...]
Template.threadlist.events({
“单击tr”:函数(事件){
Session.set(“selectedThread”,this);
}
});
//在html中
{{>数据线程}
{{{#如果线程}}

{{{#每个线程.posts} {{user}}说:{{text}
{{/每个}} {{/if}

这些变化是巨大的(非常简单!),但反应性仍然不起作用:(..

问题是您正在使用jQuery填充DOM:

$("#posts").html( Meteor.render(Template["datathread"]) );
这打破了反应链

您只显示了部分代码,因此我无法给出完整的解决方案,但datathread模板似乎已经在使用selectedThread会话变量——这很好。因此,使用
{>datathread}
替换HTML中的
#posts
元素同样简单

编辑:

您需要更改的一件事是您假设的范围
datathread
:如果我理解正确,它已经是线程本身了:

<template name="datathread">
     <hr />
     {{#each posts}}
         {{user}} says: {{text}}
         <br />
     {{/each}}
</template>
对于
数据线程

Template.datathread.thread = function(){
    return Threads.find({_id: Session.get("selectedThread")});
}

或者类似的问题。

谢谢你的回答!现在,我用新的代码编辑了这个问题…我被困在这里了…如果你需要更多的代码,告诉我,我将它粘贴到帖子中。谢谢!!!!!这个解决方案非常好!抱歉,如果这是一个愚蠢的问题…六小时前,我开始学习Meteor…再次感谢你!不客气。Meteor/react你的头脑需要一段时间才能清醒过来,但一旦清醒过来,一切都是有意义的:——)@ChristianFritz请看一下: