forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resources can be loaded with ajax etc
Resources can be loaded with ajax and resources can be arrays in event objects. Thanks for thebandit for the pull request. Example options: Resources via ajax: resources: 'http://localhost/fullcalendar/tests/json-resources.php', Resources as arrays in events: events: [ { title: 'All Day Event in resource 2 and 3', start: new Date(y, m, 1), resource: ['resource2','resource3'] } ],
- Loading branch information
Jarno Kurlin
committed
Sep 16, 2012
1 parent
d0743d3
commit 0542538
Showing
13 changed files
with
178 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
header('Content-type: application/json'); | ||
echo '[{"name":"Resource 2","id":"resource2"},{"name":"Resource 1","id":"resource1"}]'; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Responsible for resources. Resource source object is anything that provides | ||
* data about resources. It can be function, a JSON object or URL to a JSON | ||
* feed. | ||
*/ | ||
|
||
|
||
function ResourceManager(options) { | ||
var t = this; | ||
|
||
// exports | ||
t.fetchResources = fetchResources; | ||
|
||
// local | ||
var sources = []; // source array | ||
var cache; // cached resources | ||
|
||
_addResourceSources(options['resources']); | ||
|
||
|
||
/** | ||
* ---------------------------------------------------------------- | ||
* Categorize and add the provided sources | ||
* ---------------------------------------------------------------- | ||
*/ | ||
function _addResourceSources(_sources) { | ||
var source = {}; | ||
|
||
if ($.isFunction(_sources)) { | ||
// is it a function? | ||
source = { | ||
resources: _sources | ||
}; | ||
sources.push(source); | ||
} else if (typeof _sources == 'string') { | ||
// is it a URL string? | ||
source = { | ||
url: _sources | ||
}; | ||
sources.push(source); | ||
} else if (typeof _sources == 'object') { | ||
// is it json object? | ||
for (var i=0; i<_sources.length; i++) { | ||
var s = _sources[i]; | ||
normalizeSource(s); | ||
source = { | ||
resources: s | ||
}; | ||
sources.push(source); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* ---------------------------------------------------------------- | ||
* Fetch resources from source array | ||
* ---------------------------------------------------------------- | ||
*/ | ||
function fetchResources(useCache) { | ||
// if useCache is not defined, default to true | ||
useCache = typeof useCache !== 'undefined' ? useCache : true; | ||
|
||
if (cache != undefined && useCache) { | ||
// get from cache | ||
return cache; | ||
} else { | ||
// do a fetch resource from source, rebuild cache | ||
cache = []; | ||
var len = sources.length; | ||
for (var i = 0; i < len; i++) { | ||
var resources = _fetchResourceSource(sources[i]); | ||
cache = cache.concat(resources); | ||
} | ||
return cache; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* ---------------------------------------------------------------- | ||
* Fetch resources from each source. If source is a function, call | ||
* the function and return the resource. If source is a URL, get | ||
* the data via synchronized ajax call. If the source is an | ||
* object, return it as is. | ||
* ---------------------------------------------------------------- | ||
*/ | ||
function _fetchResourceSource(source) { | ||
var resources = source.resources; | ||
if (resources) { | ||
if ($.isFunction(resources)) { | ||
return resources(); | ||
} | ||
} else { | ||
var url = source.url; | ||
if (url) { | ||
$.ajax({ | ||
url: url, | ||
dataType: 'json', | ||
cache: false, | ||
success: function(res) { | ||
res = res || []; | ||
resources = res; | ||
}, | ||
error: function() { | ||
alert("ajax error getting json from "+url); | ||
}, | ||
async: false // too much work coordinating callbacks so dumb it down | ||
}); | ||
} | ||
} | ||
return resources; | ||
} | ||
|
||
|
||
/** | ||
* ---------------------------------------------------------------- | ||
* normalize the source object | ||
* ---------------------------------------------------------------- | ||
*/ | ||
function normalizeSource(source) { | ||
if (source.className) { | ||
if (typeof source.className == 'string') { | ||
source.className = source.className.split(/\s+/); | ||
} | ||
}else{ | ||
source.className = []; | ||
} | ||
var normalizers = fc.sourceNormalizers; | ||
for (var i=0; i<normalizers.length; i++) { | ||
normalizers[i](source); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
header('Content-type: application/json'); | ||
echo '[{"name":"Resource 3","id":"resource3"},{"name":"Resource 2","id":"resource2"},{"name":"Resource 1","id":"resource1"}]'; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters