简体   繁体   English

从方法调用回调访问meteor Template.instance().data

[英]Accessing meteor Template.instance().data from method call callback

I have such piece of code im meteor template manager:我在流星模板管理器中有这样一段代码:

"click #refuse": function() {
Meteor.call("removeUserFromEvent", this._id, Meteor.userId());
if (Template.instance().data.participants.length === Template.instance().data.orderedParticipants.length) {
  Meteor.call("updateEventStatus", this._id, "ordered");
}
Router.go("/");

} }

And I would like to make that if(...){...} stuff executed in a Meteor.call(...) callback, like我想让if(...){...}Meteor.call(...)回调中执行的东西,比如

"click #refuse": function() {
Meteor.call("removeUserFromEvent", this._id, Meteor.userId(), function(){
  if (Template.instance().data.participants.length === Template.instance().data.orderedParticipants.length) {
  Meteor.call("updateEventStatus", this._id, "ordered");
}
});
Router.go("/");
}

But if I try to do this it turns out that inside of that callback Template.instance() returns null and I can't get data from the template.但是,如果我尝试这样做,结果是在回调Template.instance()返回null并且我无法从模板中获取数据。

How can I put such stuff (I mean, get some current state parameters and depending on those call or not call another method) in method callback?我怎样才能在方法回调中放置这些东西(我的意思是,获取一些当前状态参数并取决于这些调用或不调用另一个方法)? Maybe Template.instance().data is a wrong place for storing state parameters?也许 Template.instance().data 是存储状态参数的错误位置? Is Template.instance.data reactive at all? Template.instance.data 是反应式的吗? Maybe I should change the architecture somehow to make it possible for such functionality to reside in a callback?也许我应该以某种方式更改架构以使此类功能可以驻留在回调中?

That's a event handler and event handler functions accept event and template as parameters as in 'event target': function(event, template) {} therefore your code could be revised as:这是一个事件处理程序,事件处理程序函数接受事件和模板作为参数,如'event target': function(event, template) {}因此您的代码可以修改为:

"click #refuse": function(evt,tmp) {
  // get and cache your template data context references;
  var participants = tmp.data.participants;
  var orderedParticipants = tmp.data.orderedParticipants;

  // this._id is not reliable, you should use Blaze.getData() on the event target
  var _id = Blaze.getData(event.currentTarget)._id;

  var userId = Meteor.userId();

  // make sure your callback function accepts error and result 
  Meteor.call("removeUserFromEvent", _id, userId, function(err,res) {

    if (err) {/* handle error */}

    if (res) {

      if (participants.length === orderedParticipants.length) {

        Meteor.call("updateEventStatus", _id, "ordered", function(err,res) {

          if (err) {/* handle error */}

          if (res) {
            // perhaps you would like to redirect to home after successful operation only
            Router.go("/");
          }

        });
      }

    }

  });

}

PS: There are too many callbacks here, so you may want to look into promises to simplify this code. PS:这里的回调太多了,所以你可能想看看promise来简化这段代码。

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

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