我使用fullcalendar作为做一些事情的插件。我正在按照文档中的指示初始化日历:
_initializeCalendar: function() {
let me = this;
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
navLinks: true, // can click day/week names to navigate views
editable: true,
eventLimit: true, // allow "more" link when too many events
events: me._createEvents(me.releaseStore),
eventClick: me._onEventClick
});
},
我需要改变传递给eventClick属性的函数的范围,因为我的_onEventClick函数将使用显然不在单击事件发生的划分范围内的函数。下面是_onEventClick函数:
_onEventClick: function(calEvent, jsEvent, view) {
console.log(scope);
let modal = Ext.get('modal');
let html = `
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>${calEvent.title}</h2>
</div>
<div class="modal-body">
<p>Name: ${calEvent.title}</p>
<p>Release Date: ${calEvent.start}</p>
<p>Team: ${calEvent.team}</p>
</div>
</div>
`;
modal.setHTML(html);
modal.setVisible(true);
// closes modal when X close button is clicked
$('.close').click(function() {
let modal = Ext.get('modal');
modal.setVisible(false);
});
}
所以,有没有办法传递全局作用域到fullcalendar的eventClick属性?谢谢你。
解决方案是在将函数传递给object属性时使用bind。在代码中,它看起来像这样:
clickEvent: me._onEventClick.bind(me)
这里"me"在_initializeCalendar中被设置为"this",所以"this"具有全局作用域