2017-03-27 94 views
0

我在我的流星應用以下邏輯:流星火焰獲取數據,並更新模板

Template.configs.events = { 
     'click li': function(e, template) { 
      Meteor.call('getMaster', uri, function(err, response) { 
      // Template.set(response) 
     }); 
     } 
    }; 

列表上的用戶點擊後,一個JSON對象通過Ajax返回。我將如何動態地將這個對象注入模板?有沒有設計模式?

預先感謝您。

回答

2

您可能會通過模板的反應變量處理此類返回數據。使用你的代碼,我寫了一個「全」的例子顯示了初始化,設置和獲取此類瓦爾的:

Template.configs.onCreated(function() { 
    this.foo = new ReactiveVar(); 
    this.bar = new ReactiveVar(); 
}); 

Template.configs.helpers({ 
    foo() { 
     return Template.instance().foo.get(); 
    }, 

    bar() { 
     return Template.instance().bar.get(); 
    } 
}); 

Template.configs.events({ 
    'click li': function(e, template) { 
     Meteor.call('getMaster', uri, function(err, response) { 
      let foo = response.foo; 
      let bar = response.bar; 

      template.foo.set(foo); 
      template.bar.set(bar); 
     }); 
    } 
}); 
+0

謝謝你,我有點設法建立同樣的解決方案,但與無功字典,而不是反應變種。雖然我無法弄清楚其中的差異。 – AppRoyale

+0

他們是相似的。字典只是簡單地給出了一種被動的方法來將一堆鍵/值對象保存爲一個單一的東西。例如也許你有一些未知數量的反應式汽車;你不能爲每個變量創建一個反應變量,因爲你事先不知道你會有多少人。 – zim