从内部调用JavaScript函数事件

时间:2012-04-17 14:13:16

标签: javascript function javascript-events

我想知道是否可以在同一个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));

    }
});

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

是的,你可以。将函数放在对象外部的变量(最好是命名空间,以免污染全局命名空间),然后从你想要的地方调用它:

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

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

您可能希望更加松散地与任何类型的发布/订阅系统结合使用。请参阅:https://gist.github.com/1321768