2014-01-19 21 views
0

我有一個經典的「線程 - >帖子」模型。 在應用程序中,我有一個「線程」集合的左邊欄。當用戶點擊一個線程時,我想用Thread-> Posts元素創建另一個div。渲染與流星動態創建新的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> 

( 「用戶」 和 「文本」 propertis是從「thread.posts」元素)

使用此代碼,我只獲取刷新(F5)網頁(或執行'click tr'event')的新值。我做錯了什麼?

謝謝!

== ==編輯

好吧......現在,隨着Chistian弗裏茨recomendation,我的代碼它是這樣的:

// 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> 

的變化都很大(它是如此簡單!),但反應仍然不工作:(....

回答

2

的問題是,你正在使用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> 

此外,this的對答事件處理程序肯定不會是線程。我不知道tr的數據在threadlist(你能證明代碼?),但你很可能會做一些這樣的:

Session.set("selectedThread", this.id); 

而對於datathread

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

或類似的。

+0

謝謝你的回答!現在,我用新的代碼編輯了這個問題....我堅持這樣做......如果您需要更多代碼,請告訴我,然後將其粘貼到帖子中。 – Nullpo

+0

好的,看我的編輯。 –

+0

謝謝!!!!!這個解決方案非常好用對不起,如果這是一個愚蠢的問題...六個小時前,我已經開始學習流星...再次感謝你! – Nullpo