2012-04-17 161 views
0

我想知道是否可以在同一個JS文件中調用「eventNew」函數。正如我試圖從不同的功能訪問「eventNew」功能。從內部調用JavaScript函數事件

在此先感謝

下面是代碼的一部分:

$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)); 

    } 
}); 

回答

0

是的,你可以。把對象外的函數上的變量(最好是命名空間,這樣不會污染全局命名空間),然後調用,從那裏你想:

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

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

您可能要做到這一點更加鬆耦合發佈/任何形式的子系統。參見:https://gist.github.com/1321768

0

eventNew功能的匿名對象上定義,所以在技術上,你將能夠從定義的函數調用它在同一個對象上,如this.eventNew(...)