Prechádzať zdrojové kódy

switch away from jquery ajax

Adam Shaw 7 rokov pred
rodič
commit
485346ea44

+ 6 - 6
plugins/gcal/GcalEventSource.ts

@@ -1,5 +1,5 @@
-import * as $ from 'jquery'
-import { EventSource, JsonFeedEventSource, warn, applyAll } from 'fullcalendar'
+import * as reqwest from 'reqwest'
+import { EventSource, JsonFeedEventSource, warn, applyAll, assignTo } from 'fullcalendar'
 
 
 export default class GcalEventSource extends EventSource {
@@ -43,12 +43,14 @@ export default class GcalEventSource extends EventSource {
 
     this.calendar.pushLoading()
 
-    $.ajax($.extend(
+    reqwest(assignTo(
       {}, // destination
       JsonFeedEventSource.AJAX_DEFAULTS,
       ajaxSettings,
       {
         url: url,
+        type: 'jsonp',
+        jsonpCallback: 'callback',
         data: requestParams,
         success: (responseData, status, xhr) => {
           let rawEventDefs
@@ -115,9 +117,7 @@ export default class GcalEventSource extends EventSource {
 
 
   buildUrl() {
-    return GcalEventSource.API_BASE + '/' +
-      encodeURIComponent(this.googleCalendarId) +
-      '/events?callback=?' // jsonp
+    return GcalEventSource.API_BASE + '/' + encodeURIComponent(this.googleCalendarId) + '/events'
   }
 
 

+ 10 - 9
src/models/event-source/JsonFeedEventSource.ts

@@ -1,4 +1,4 @@
-import * as $ from 'jquery'
+import * as reqwest from 'reqwest'
 import { assignTo } from '../../util/object'
 import { applyAll } from '../../util/misc'
 import EventSource from './EventSource'
@@ -7,8 +7,7 @@ import EventSource from './EventSource'
 export default class JsonFeedEventSource extends EventSource {
 
   static AJAX_DEFAULTS = {
-    dataType: 'json',
-    cache: false
+    method: 'GET'
   }
 
   // these props must all be manually set before calling fetch
@@ -45,7 +44,7 @@ export default class JsonFeedEventSource extends EventSource {
 
     this.calendar.pushLoading()
 
-    $.ajax(assignTo(
+    reqwest(assignTo(
       {}, // destination
       JsonFeedEventSource.AJAX_DEFAULTS,
       ajaxSettings,
@@ -53,12 +52,14 @@ export default class JsonFeedEventSource extends EventSource {
         url: this.url,
         data: requestParams,
         success: (rawEventDefs, status, xhr) => {
-          let callbackRes
+          this.calendar.popLoading() // good to do this before onSuccess
 
-          this.calendar.popLoading()
+          if (typeof rawEventDefs === 'string') {
+            rawEventDefs = JSON.parse(rawEventDefs)
+          }
 
           if (rawEventDefs) {
-            callbackRes = applyAll(origAjaxSuccess, this, [ rawEventDefs, status, xhr ]) // redirect `this`
+            let callbackRes = applyAll(origAjaxSuccess, this, [ rawEventDefs, status, xhr ]) // redirect `this`
 
             if (Array.isArray(callbackRes)) {
               rawEventDefs = callbackRes
@@ -70,7 +71,7 @@ export default class JsonFeedEventSource extends EventSource {
           }
         },
         error: (xhr, statusText, errorThrown) => {
-          this.calendar.popLoading()
+          this.calendar.popLoading() // good to do this before onFailure
 
           applyAll(origAjaxError, this, [ xhr, statusText, errorThrown ]) // redirect `this`
           onFailure()
@@ -104,7 +105,7 @@ export default class JsonFeedEventSource extends EventSource {
       timezoneParam = calendar.opt('timezoneParam')
     }
 
-    // retrieve any outbound GET/POST $.ajax data from the options
+    // retrieve any outbound GET/POST data from the options
     if (typeof ajaxSettings.data === 'function') {
       // supplied as a function that returns a key/value object
       customRequestParams = ajaxSettings.data()

+ 6 - 12
tests/manual/sources.html

@@ -15,11 +15,11 @@
   var d = date.getDate();
   var m = date.getMonth();
   var y = date.getFullYear();
-  
+
   var gcalFeed = $.fullCalendar.gcalFeed("http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic");
-  
+
   var jsonFeed = "../demos/json-events.php";
-  
+
   var staticEvents = [
     {
       title: 'All Day Event',
@@ -98,7 +98,7 @@
       start: '1295078400.0'
     }
   ];
-  
+
   var customSource = function(start, end, callback) {
     callback([
       {
@@ -111,12 +111,6 @@
       }
     ]);
   };
-  
-  /* TODO: doesn't work anymore
-  $.ajaxSetup({
-    cache: true // should NOT insert an extra _ parameter
-  });
-  */
 
   $(document).ready(function() {
     cal = $('#calendar').fullCalendar({
@@ -156,11 +150,11 @@
   .red-event {
     background: red !important;
   }
-  
+
   .yellow-event {
     background: yellow !important;
   }
-  
+
   .black-text-event {
     color: #000 !important;
   }