【问题标题】:Meteor call template method from another methodMeteor 从另一个方法调用模板方法
【发布时间】:2014-07-06 12:46:45
【问题描述】:

我想在客户端的方法中调用一个方法,但我不知道如何处理它,我试过像myFunction()this.myFunction()这样调用,但它不起作用......这是我的代码

Template.decision.rendered = function () {
    if ($(".active").length == 0) {
        var random = Math.floor(Math.random() * 1000);
        var $items = $(".item");
        $items.eq(random % $items.length).addClass("active");
    }
    $.each($(".item"), function (index, value) {
        if (Session.get($(this).attr('id'))) {
            this.showResults(value.option);
        }
    });
};
Template.decision.showResults = function($option) {
    $('#result').html('Option ' + $option + ' is voted');
};

如您所见,我想为 rendered 回调中的每个项目调用 showResults...

【问题讨论】:

    标签: javascript jquery methods callback meteor


    【解决方案1】:

    使用 Template.decision.showResults(); silly me 找到它。

    【讨论】:

      【解决方案2】:

      我认为根据您的尝试,更好的方法是使用 Session 变量或 Meteor 方法:

      会话变量。

      Template.decision.created = function() {
        Session.setDefault('showResults', false);
      }
      
      Template.decision.rendered = function() {
        // [...]
      
        $.each($(".item"), function (index, value) {
          if (Session.get($(this).attr('id'))) {
            Session.set('showResults', true);
          }
        });
      }
      
      Template.decision.showResults = function() {
         return Session.get('showResults');
      }
      
      // in your template
      <template name="decision">
        {{#if showResults}}
          <p>Here is the results.</p>
        {{/if}}
      </template>
      

      流星法。

      // On the client.
      Template.decision.rendered = function() {
        // [...]
      
        $.each($(".item"), function (index, value) {
          if (Session.get($(this).attr('id'))) {
            Meteor.call('showResults', function(error, result) {
              if (!error and result === true) {
                $('.class').hide()  // It is here that you modify the DOM element according to the server response and your needs.
              }
            });
          }
        });
      }
      
      // Server-side method
      // But, if you want it to react like a stub, put it inside your lib folder.
      Meteor.methods({
        showResults: function() {
          // do your thing
          result = true; // let's say everything works as expected on server-side, we return true
          return result;
        }
      });
      

      【讨论】:

      • 但是如果我想在 showResults 方法中编辑 DOM 元素怎么办?它不会在 Meteor.methods 中工作,对吧?比如说Console.log("Voted");我想做$('#voted').html('Voted');
      • 您不能在 Meteor.methods 中编辑 DOM 元素(因为您是服务器端并且 jQuery 仅在客户端上可用)但这将在 Meteor.call 回调中起作用(因为代码在客户端运行-side)。
      • 你能给我看一个 Meteor.call 回调来操作 DOM 元素的例子吗?因为这是最终目的,所以我会用一个例子来更新我的问题..
      • 基本上就是我问的,如何调用方法来操作DOM元素,我不想要任何复杂的东西,只是从方法中操作一个DOM。
      猜你喜欢
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 2015-06-16
      相关资源
      最近更新 更多