语义手风琴让我发疯! 有人知道在Meteor实施时是否有办法获得手风琴的当前状态(例如开放或关闭)? 如果我理解正确,我应该在Template.foo.helpers部分的.js文件中创建一个函数。到目前为止我所做的是:
isOpen : function() {
var cState = $('.ui.accordion').currentState();
return cState=='open';
}
如果手风琴是打开的,则应该返回true,否则返回false,但它似乎不起作用。我究竟做错了什么?有没有办法做这样的工作?
提前致谢!
答案 0 :(得分:1)
问题是你的助手没有引用被动数据源,这意味着只要手风琴的状态发生变化就不会重新执行。
您可以通过使用Semantic UI accordion插件回调来跟踪窗口小部件的当前状态并将其存储为活动数据源来解决此问题。
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();
}
});