Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple eventSources #32

Closed
dittodhole opened this issue Feb 9, 2011 · 0 comments
Closed

multiple eventSources #32

dittodhole opened this issue Feb 9, 2011 · 0 comments

Comments

@dittodhole
Copy link

atm you cannot declare multiple eventSources.

therefore i've written a little generic eventManager:

var eventManager = {
    fetchData: function (sources, callback) {
        var sourcesLength = sources.length;

        var pendingSourcesCount = sourcesLength;
        var currentData = new Array();
        for (var i = 0; i < sourcesLength; i++) {
            var source = sources[i];
            this._fetchDataFromSource(source, function (data) {
                currentData = currentData.concat(data);
                pendingSourcesCount--;
                if (!pendingSourcesCount) {
                    callback(currentData);
                }
            });
        }
    },
    _fetchDataFromSource: function (source, callback) {
        if (typeof source == 'string') {
            $.ajax({
                'url': source,
                'dataType': 'json',
                'success': function (events) {
                    callback(events);
                }
            });
        }
        else if ($.isFunction(source)) {
            source(function (events) {
                callback(events);
            });
        }
        else {
            callback(source);
        }
    }
};

which can be used eg:

    var $calendar = $('#selector').weekCalendar({
        'data': function (start, end, callback) {
            eventManager.fetchData([
                function (internalCallback) {
                    var url = 'help1.wcf';
                    var data = {
                        'start': new Date(),
                        'end': new Date()
                    };
                    $.getJSON(url, data, function (data) {
                        internalCallback(data);
                    });
                },
                function (internalCallback) {
                    var url = 'help2.wcf';
                    var data = {
                        'start': new Date(),
                        'end': new Date()
                    };
                    $.getJSON(url, data, function (data) {
                        internalCallback(data);
                    });
                }
            ], callback);
        }
    });

maybe sth like this shoule be integrated...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant