forked from fullcalendar/fullcalendar
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for json resource feed
- Loading branch information
Showing
7 changed files
with
171 additions
and
31 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
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,22 @@ | ||
<?php | ||
|
||
echo json_encode(array( | ||
|
||
array( | ||
'id' => 1, | ||
'name' => "JSON Resource 1" | ||
), | ||
|
||
array( | ||
'id' => 2, | ||
'name' => "JSON Resource 2" | ||
), | ||
|
||
array( | ||
'id' => 3, | ||
'name' => "JSON Resource 3" | ||
) | ||
|
||
)); | ||
|
||
?> |
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,136 @@ | ||
/* | ||
* 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() { | ||
console.error("ajax error"); | ||
}, | ||
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