0

我想知道是否可以在同一个 JS 文件中调用“eventNew”函数。当我试图从不同的函数访问“eventNew”函数时。

这是代码的一部分:

$calendar.weekCalendar({
    displayOddEven: true,
    timeslotsPerHour: 4,
    allowCalEventOverlap: true,
    overlapEventsSeparate: true,
    firstDayOfWeek: 1,
    businessHours: { start: 1, end: 24, limitDisplay: true },
    daysToShow: 7,
    switchDisplay: { '1 day': 1, '3 next days': 3, 'work week': 5, 'full week': 7 },
    title: function (daysToShow) {
        return daysToShow == 1 ? '%date%' : '%start% - %end%';
    },
    height: function ($calendar) {
        return $(window).height() - $("h1").outerHeight() - 1;
    },
    eventNew: function (calEvent, $event) {
        var $dialogContent = $("#event_edit_container");
        resetForm($dialogContent);
        var startField = $dialogContent.find("select[name='start']").val(calEvent.start);
        var endField = $dialogContent.find("select[name='end']").val(calEvent.end);
        var titleField = $dialogContent.find("input[name='title']");
        var bodyField = $dialogContent.find("textarea[name='body']");


        $dialogContent.dialog({
            modal: true,
            title: "New Calendar Event",
            close: function () {
                $dialogContent.dialog("destroy");
                $dialogContent.hide();
                $('#calendar').weekCalendar("removeUnsavedEvents");
            },
            buttons: {
                save: function () {
                    calEvent.id = id;
                    id++;
                    calEvent.start = new Date(startField.val());
                    calEvent.end = new Date(endField.val());
                    calEvent.title = titleField.val();
                    calEvent.body = bodyField.val();

                    SaveEvent(); // Modified

                    $calendar.weekCalendar("removeUnsavedEvents");
                    $calendar.weekCalendar("updateEvent", calEvent);
                    $dialogContent.dialog("close");
                },
                cancel: function () {
                    $dialogContent.dialog("close");
                }
            }
        }).show();

        $dialogContent.find(".date_holder").text($calendar.weekCalendar("formatDate", calEvent.start));
        setupStartAndEndTimeFields(startField, endField, calEvent, $calendar.weekCalendar("getTimeslotTimes", calEvent.start));

    }
});
4

2 回答 2

0

您的eventNew函数是在匿名对象上定义的,因此从技术上讲,您将能够从定义在同一对象上的函数调用它,如this.eventNew(...).

于 2012-04-17T14:20:22.330 回答
0

是的你可以。将函数放在对象外部的变量上(最好是命名空间,以免污染全局命名空间),然后从您希望的位置调用它:

var namespace.myEventNew = function (calEvent, $event) {
   [code here]
}

$calendar.weekCalendar({
  ...
  eventNew : function (calEvent, $event) {
      namespace.myEvent(calEvent, $event);
  },
  ...
});

您可能希望更松散地与任何类型的发布/订阅系统耦合。见:https ://gist.github.com/1321768

于 2012-04-17T14:22:56.837 回答