2012-12-02 49 views
0

我使用meteor創建一個簡單的數據錄入和可視化應用程序。當我嘗試使用jQuery根據數據更改每個條目的某些css時,我遇到了問題。流星和jQuery:使用cursor.observe觸發jQuery css操作

我已經設置好了,當添加一個條目(使用cursor.observe)計算高度值時,然後使用jQuery將該值應用於css,但實際上並沒有這樣做。我知道值正在計算正確,因爲我使用控制檯來顯示它們。

這是因爲在該代碼運行時,這些條目的實際html還沒有從模板中構建?如果是這樣的話,我可以在哪裏粘貼這段代碼,以便它在html之後運行jQuery?

下面是與這個問題相關的代碼,任何幫助將不勝感激。

Template.mainPanel.Actions = function(){ 
    var actions = Actions.find() 

    actions.observe({ 
     added: function(action){ 
      var entryHeight = action.Duration 

      console.log("entryHeight: " + entryHeight) 

      $('#vis' + action.ID).css({'height':entryHeight}) 
     } 
    }) 

    return actions 
} 
+0

使用'Template.rendered'回調。 http://docs.meteor.com/#template_rendered – bento

回答

2

我假設你有一些代碼在您的應用程序,如:

<template name="mainPanel"> 
    {{#each Actions}} 
    <div id="{{ID}}">Something</div> 
    {{/each}} 
</template> 

,你配對了返回你的行動從Template.mainPanel.Actions光標。

而是做一些複雜的與觀察,您可以包括樣式反應車把直接在你的HTML,像這樣:

<template name="mainPanel"> 
    {{#each Actions}} 
    <div id="{{ID}}" style="height: {{Duration}}px">Something</div> 
    {{/each}} 
</template>