获取流星中语义手风琴的当前状态

Getting the current state of a semantics accordion in meteor

语义手风琴快把我逼疯了! 有人知道在 Meteor 中实现时是否有办法获取手风琴的当前状态(例如打开或关闭)吗? 如果我理解正确的话,我应该在 Template.foo.helpers 节内的 .js 文件中创建一个函数。到目前为止我所做的是:

    isOpen : function() {
        var cState = $('.ui.accordion').currentState();
        return cState=='open';
    }

如果手风琴打开,这应该 return 为真,否则为假,但它似乎不起作用。我究竟做错了什么?有没有办法做这样的工作?

提前致谢!

问题是您的助手没有引用响应式数据源,这意味着它不会在手风琴状态更改时重新执行。

您可以通过使用语义 UI 手风琴插件回调来跟踪小部件的当前状态并将其存储为反应性数据源来解决此问题。

Template.accordion.onCreated(function(){
  // you'll need to meteor add reactive-var to use this
  this.opened = new ReactiveVar(false);
});

Template.accordion.onRendered(function(){
  // store a reference to the template instance to use it later
  // in functions where the this keyword will be bound to something else
  var template = this;
  this.$(".ui.accordion").accordion({
    onOpen:function(){
      // here, the this keyword is bound to the currently opened item
      template.opened.set(true);
    },
    onClose:function(){
      // modify the reactive var accordingly
      template.opened.set(false);
    }
  });
});

Template.accordion.helpers({
  opened:function(){
    // Template.instance().opened is a reactive data source
    // this helper will get re-executed whenever its value is modified
    return Template.instance().opened.get();
  }
});