Skip to content

Commit

Permalink
Added new boolean option refetchResources which determines do we fetc…
Browse files Browse the repository at this point in the history
…h resources every time we get new events. refetchResources adds calendar start and end times to ajax request so we can have different resources in different times.
  • Loading branch information
Jarno Kurlin committed Oct 24, 2012
1 parent 3eb9e98 commit 483abaf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ function Calendar(element, options, eventSources, resourceSources) {
function updateEvents(forceRender) {
if (!options.lazyFetching || isFetchNeeded(currentView.visStart, currentView.visEnd)) {
refetchEvents();
if (options['refetchResources']) refetchResources(); // refetch resources every time new events are loaded
}
else if (forceRender) {
rerenderEvents();
Expand All @@ -331,8 +332,8 @@ function Calendar(element, options, eventSources, resourceSources) {
}

function refetchResources() {
fetchResources(false);
fetchResources(false, currentView);

// we have to destroy all view instances and recreate current one
viewInstances = [];

Expand Down
27 changes: 20 additions & 7 deletions src/ResourceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function ResourceManager(options) {

// exports
t.fetchResources = fetchResources;

// local
var sources = []; // source array
var cache; // cached resources
Expand Down Expand Up @@ -57,7 +57,7 @@ function ResourceManager(options) {
* Fetch resources from source array
* ----------------------------------------------------------------
*/
function fetchResources(useCache) {
function fetchResources(useCache, currentView) {
// if useCache is not defined, default to true
useCache = typeof useCache !== 'undefined' ? useCache : true;

Expand All @@ -69,7 +69,7 @@ function ResourceManager(options) {
cache = [];
var len = sources.length;
for (var i = 0; i < len; i++) {
var resources = _fetchResourceSource(sources[i]);
var resources = _fetchResourceSource(sources[i], currentView);
cache = cache.concat(resources);
}
return cache;
Expand All @@ -85,17 +85,30 @@ function ResourceManager(options) {
* object, return it as is.
* ----------------------------------------------------------------
*/
function _fetchResourceSource(source) {
function _fetchResourceSource(source, currentView) {
var resources = source.resources;

if (resources) {
if ($.isFunction(resources)) {
return resources();
}
} else {
var url = source.url;
if (url) {
$.ajax({
url: url,
var data={};
if (typeof currentView == 'object') {
var startParam = options.startParam;
var endParam = options.endParam;
if (startParam) {
data[startParam] = Math.round(+currentView.visStart / 1000);
}
if (endParam) {
data[endParam] = Math.round(+currentView.visEnd / 1000);
}
}

$.ajax($.extend({}, source, {
data: data,
dataType: 'json',
cache: false,
success: function(res) {
Expand All @@ -106,7 +119,7 @@ function ResourceManager(options) {
alert("ajax error getting json from "+url);
},
async: false // too much work coordinating callbacks so dumb it down
});
}));
}
}
return resources;
Expand Down
3 changes: 3 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var defaults = {
lazyFetching: true,
startParam: 'start',
endParam: 'end',

// resource ajax
refetchResources: false,

// time formats
titleFormat: {
Expand Down

0 comments on commit 483abaf

Please sign in to comment.