簡體   English   中英

訪問Meteor事件處理程序中的模板助手字典

[英]Accessing template helper dictionary in Meteor event handler

在Meteor中,我將數據庫中的兩個對象發送到模板:

Template.myTemplate.helpers({
  helper1: function() {
    var object1 = this;  // data context set in iron:router...path is context dependent
    // modify some values in object1
    return this;
  },
  helper2: function() {
    return Collection2.find({_id: this.object2_id});
  }
});

該模板還有一個事件處理程序來修改上面的兩個對象。 我試圖從上面訪問helper1和helper2,但如果我調用模板的數據上下文,我只能訪問未修改版本的object1。 如何訪問上面定義的幫助程序?

Template.myTemplate.events({
  'submit form': function(event) {
    event.preventDefault();
    // Access helper2 object and attributes here instead of calling Collection2.find() again
  }
});

幫助程序只是函數,因此可以隨意傳遞並分配給其他變量,因此您可以定義一個函數,然后為其分配模板助手的helper2鍵,並通過事件處理程序的原始引用進行調用。

var helperFunction = function() {
    return Collection2.find({_id: this.object2_id});
};

Template.myTemplate.helpers({
    helper1: function() {
        var object1 = this;  // data context set in iron:router...path is context dependent
        // modify some values in object1
        return this;
    },
    helper2: helperFunction
});

Template.myTemplate.events({
    'submit form': function(event) {
        event.preventDefault();
        var cursor = helperFunction();
    }
});

如果你可以從事件中修改助手,那么,Meteor應用程序的任何部分都可以做到,這違背了Blaze的設計理念!

Blaze旨在成為單向數據綁定誘惑系統。 你要求的是使用Angular(單獨使用,或與Blaze並排使用,看看THIS ),這本質上是一個雙向數據綁定誘惑系統。

您可能還想檢查React,它也是一種雙向數據綁定

暫無
暫無

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

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