簡體   English   中英

變量賦值后,Meteor.js模板不會更新

[英]Meteor.js Template does not update after variable assignment

我正在用Meteor構建一個免費的電視跟蹤應用程序,一路上我似乎撞到了牆。 我的模板的一部分包含:

<template name="results">
  <div class="row">
      <div class="span6 offset6">
        <h4>Search Results</h4>
          <ul>
            {{#each series}}
            <li><a href="http://thetvdb.com/?tab=episode&seriesid{{tvdbseriesid}}&lid={{tvdblid}}">{{seriesname}}</li>
            {{/each}}
          </ul>
      </div>
    </div>
</template>

然后,在我的客戶端js代碼中,我這樣做:

Template.search.events({
  'click #search' : function(evt) {
    evt.preventDefault();

    var query = $('#query').val();
    if (query) {
      Meteor.call('queryshow', query, function(error, result) {
        Template.results.series = result;
        console.log(Template.results());
      });
    }
  }
});

“queryshow”服務器方法只是一個Collection查詢方法,它返回一個包含模板所需數據的對象。

但問題是:更改未反映在瀏覽器窗口中。 我似乎無法弄清楚為什么,因為下面顯示的console.log(Template.results())調用返回我期望的正確的html。

我該如何解決? 我試着查看Meteor的文檔,我似乎找不到強行重新呈現模板的功能。 任何幫助都感激不盡!

Template.results.series應該是一個返回系列的函數,而不是直接設置到你想要使用的系列。 這樣,當Meteor運行函數來獲取序列時,它可以注冊函數所依賴的函數,然后在任何依賴項發生變化時重新運行該函數。

為您的信息注冊依賴項的最簡單方法是使用Session對象。 所以你的代碼看起來像:

Template.results.series = function () { return Session.get('resultsSeries'); }

Meteor.call('queryshow', query, function (err, res) {
  // handle the error if it's there
  // ...
  // Then set the session variable, which will re-run anything that depends on it.
  Session.set('resultsSeries', res)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM