简体   繁体   English

为什么我的函数不使用新点击的新参数重新运行?

[英]Why does my function not re-run with new parameter from new click?

I have four buttons that fill a shared target box in HTML. 我有四个按钮可以填充HTML中的共享目标框。 Each button sends a different argument ("type") to a common handler, which runs a common function, CalChoice, which fires an API with a customized URL (based on the type argument). 每个按钮都会向公共处理程序发送一个不同的参数(“类型”),该处理程序运行一个公共函数CalChoice,该函数会使用自定义的URL(基于类型参数)触发API。 This works correctly the first time, but any click on another button does not fire the function again with the new argument. 第一次可以正常运行,但是单击另一个按钮不会再次使用新参数触发该函数。 I've run counters and alerts to prove my clicks are getting to the function, but after the first time, I can see the XHR callback in my console does not change. 我已经运行了计数器和警报来证明我的点击已经到达该功能,但是第一次之后,我可以看到控制台中的XHR回调没有改变。 (I suppose I can't rule out the function is re-running with the old param, as content would not change, but I don't see the slightest burp.) In the code below, you'll find the click handler and func call at the very bottom below everything. (我想我不能排除该函数正在使用旧的参数重新运行,因为内容不会更改,但是我看不到丝毫打扰。)在下面的代码中,您将找到单击处理程序和func调用位于所有内容的最底部。

Why fail? 为什么失败?

 function CalChoice(type) {

    if ( type == "getArt")  {
    $useURL = "my cal source here 1"
        } else if  ( type == "getNature") {
    $useURL = "my cal source here 2"
        } else if  ( type == "getFitness") {
    $useURL = "my cal source here 3"
        } else if  ( type == "getLectures") {
    $useURL = "my cal source here 4"
        }

    $('.amu-calendar').fullCalendar({

        googleCalendarApiKey: 'XXXXXX',  //our API key
        events: { googleCalendarId: $useURL
        },

        eventBackgroundColor: '#65378e',

        defaultView: 'vertWeek',

        header: {
                    left: "title",
                    center: "vertWeek month",
                    right:  "prev next"
                    },

        dayRender: function( date, cell ) { 
            // Get the current view     
            var view = $('.amu-calendar').fullCalendar('getView');

            // Check if the view is the new vertWeek - 
            // in case you want to use different views you don't want to mess with all of them
            if (view.name == 'vertWeek') {


                // Hide the widget header - looks wierd otherwise
                $('.fc-widget-header').hide();
                $('.fc-day-header').hide();

                // $('div#calendar.fc.fc-ltr.ui-widget').find('div.fc-toolbar').find('div.fc-center').html( "<a href='#switchView'>Change View</a>" );


                // Remove the default day number with an empty space. Keeps the right height according to your font.
                $('.fc-day-number').html('<div class="fc-vertweek-day">&nbsp;</div>');

                // Create a new date string to put in place  
                var this_date = date.format('ddd, MMM Do');

                // Place the new date into the cell header. 
                cell.append('<div class="fc-vertweek-header"><div class="fc-vertweek-day">'+this_date+'</div></div>');

            }
        },
        theme: true,
        themeButtonIcons: true,
        eventClick: function(gcalEvent, jsEvent, view) {
            gcalEvent.preventDefault ? gcalEvent.preventDefault() : gcalEvent.returnValue = false;  //IE8 does not recognize preventDefault so use returnvalue=false
            var eid = gcalEvent.id;
            var etitle = gcalEvent.title;
            //var eurl = gcalEvent.url;
            //var estart = $.fullCalendar.formatDate(gcalEvent.start, 'MMMM d'+', '+'h:mm tt ');
            //var eend = $.fullCalendar.formatDate(gcalEvent.end, 'h:mm tt');
            var elocation = gcalEvent.location;
            var edescription = gcalEvent.description;
            var eallday = gcalEvent.allDay;
            $('div.title-d').html(etitle);
            //$('div.datetime-d').html(estart + "-" +eend);
            $('div.location-d').html(elocation);
            $('div.description-d').html(edescription);
            $('div.framer').css({'background' : 'rgb(234,191,73)'});    // and do the same for the detail framer                    
            gcalEvent.preventDefault ? gcalEvent.preventDefault() : gcalEvent.returnValue = false;  //IE8 does not recognize preventDefault so use returnvalue=false
            return false;
        }
    });
}
//MY HANDLER

$(".accordionButton").click(function(event) {                                       //amu-calendar
    event.stopPropagation();//tried this, does not help
    var type = $(this).attr('id');
    console.log("The " + type + " button was clicked." ) //this always works but...
    CalChoice(type);// a counter in this func does increment, yet it appears it is not running with the new type...no new result.
});

It isn't much a problem with the function call, because the argument is correct but the fullCalendar plugin is being stateful at some point and it will work with the previous calendar. 函数调用没有太大问题,因为参数正确,但是fullCalendar插件在某些时候处于有状态,并且可以与以前的日历一起使用。 You'll need to destroy the calendar first to create another one. 您需要先销毁日历才能创建另一个日历。

$('.amu-calendar').fullCalendar('destroy');

Another way would be to remove the eventsObject : 另一种方法是删除eventsObject

$('.amu-calendar').fullCalendar('removeEvents');
$('.amu-calendar').fullCalendar('addEventSource' , { googleCalendarId : 'newEventURL' });

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

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