Meteor + Bootstrap - Alert closed.bs.alert does not trigger

时间:2015-04-23 05:13:41

标签: javascript jquery twitter meteor twitter-bootstrap-3

I'm using Meteor and Bootstrap, with the nemo64:bootstrap package. In custom.bootstrap.json I have "alerts" and "alert" on.

I'm trying to capture the closed.bs.alert event in a Template events. For some reason, it won't capture.

Template.alert.events({
    'closed.bs.alert .alert': function () {
        console.log('closed'); // does not trigger
    }
});

Oddly, close.bs.alert does work:

Template.alert.events({
    'close.bs.alert .alert': function () {
        console.log('closed'); // triggers
    }
});

Also, if I add the event via jQuery, I can capture closed.bs.alert:

$('.alert').on('closed.bs.alert', function () { 
   console.log('closed'); // triggers
}); 

So, I know I have my events formatted correctly and I know the closed.bs.alert event is triggering... but for some reason I can't catch it with Template.alert.events.

Any ideas?

1 个答案:

答案 0 :(得分:1)

我潜入Bootstrap代码并找到它触发此事件的位置:alert.js, line 50

首先,我开始工作的技术:

Templates.alert.onRendered({
    $('.alert').on('closed.bs.alert', function (e) {
        console.log('closed'); // triggers =D
    });
});

我认为问题在于Bootstrap在触发事件之前会分离警报,因此$(document).on('closed.bs.alert', '.alert')之类的东西无效。我不确定100%,但我怀疑Meteor的Template.my_template.events()触发器也使用了非常类似的方法。

通常这会起作用:

$(function () {
    $('.alert').on('closed.bs.alert', function () {
        console.log('closed'); // doesn't trigger
    });
});

然而,Meteor对该计划投入了一个扳手,因为我正在从数据中加载警报,当触发时它们不在那里。

但是,通过在on()中添加jQuery样式Template.my_template.onRender()似乎可以解决问题。

此外,除非警报具有“淡入淡出”类,否则Bootstrap不会抛出该事件。您可以指定淡入淡出类而不包括“过渡”模块。如果你把它关掉,你将无法获得效果,但无论如何都会触发事件。