简体   繁体   English

流星方法将返回值分配给模板车把

[英]meteor method assign return to template handlebar

This is the method inside Meteor.startup function 这是Meteor.startup函数内部的方法

return Meteor.methods({
  whatsUp: function() {
   return "Up";
  }
});

And here is the event code triggering the method 这是触发方法的事件代码

Template.whats_up.events({
  'click .checkwhatsup': function() {
    Meteor.call('whatsUp');
  }
});

And here is a code simply doing returning a variable straight to the DOM 这是一个简单地将变量直接返回到DOM的代码

Template.whats_up.up = function() {
  var whats = "Up";
  return whats;
};

My question is how can I do the same function as on the last js code, 我的问题是我该如何执行与上一个js代码相同的功能,

but only after I click on a button. 但只有在我点击按钮后

Here is the html template + handlebars 这是html模板+把手

<template name="whats_up">
  <button class="checkwhatsup">What is Up?!</button>
  {{up}}
</template>

I think you're looking to do something like this: 我想您正在寻找做这样的事情:

Template.whats_up.events({
  'click .checkwhatsup': function() {
    Meteor.call('whatsUp', function(res) {
      Session.set('whats', res);
    });
  }
});

Template.whats_up.up = function() {
  return Session.get('whats');
};

The idea is to use a reactive data source (in this case, Session ) to handle this. 想法是使用反应性数据源(在本例中为Session )来处理此问题。 You could also update a Collection in your method and use a collection cursor in your helper function. 您也可以在方法中更新Collection ,并在辅助函数中使用Collection游标。 That way there's no callback function for the method call (and the template can update when other users call the method). 这样,方法调用就没有回调函数(并且当其他用户调用该方法时,模板可以更新)。 Depends on your actual usage case. 取决于您的实际使用情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM