Pergunta

I'm using angular-ui calendar to try and pull events from my API. The API is on a separate domain so I have to use jsonp.

I've tried various ways to get this working but can't get the events onto my calendar.

I built a service to call the API

angular.module('myapp.services', ['ngResource'])
.factory( 'SurgerySource', ['$resource', function($resource){
    return $resource(
        'http://api.mydomain.com/calendar.json?callback=JSON_CALLBACK&start=:start&end=:end',
        {start:'@start', end:'@end'},
        { jsonp_query: { method: 'JSONP', isArray: true } }
    );
}]);

In the controller for my calendar I can call it directly and get events on the calendar

angular.module('myapp.schedule', ['ui.calendar', 'ui.bootstrap', 'myapp.services'])
.controller('ScheduleCtrl', function ScheduleCtrl( $scope, SurgerySource ) {
    $scope.surgeries = SurgerySource.jsonp_query({
        start: 1396162800,
        end: 1399791600
    });
    $scope.uiConfig = { ... };
    $scope.eventSources = [$scope.surgeries];
});

This populates the calendar with the hardcoded date range. I couldn't figure a way to get the start and end range out of the calendar view to use though, so I tried to use an event source that calls a function (as per the documentation here http://angular-ui.github.io/ui-calendar/) This gets the date range from the view, but I can't get the returned json back into the $scope.

 $scope.eventSource = function (start, end, callback) {
     s = new Date(start).getTime() / 1000;
     e = new Date(end).getTime() / 1000;
     var eventPromise = SurgerySource.jsonp_query({
         start: s,
         end: e
     });
     callback(eventPromise);
 };

If I inspect eventPromise, I see it is an abject with the data in there (as an array?) along side a $promise object and $resolved:

[
  0 -> Resource
  1 -> Resource
  2 -> Resource
  $promise -> Object
  $resolved -> true
]

The callback doesn't do anything though and the events are not put onto the calendar.

I also tried to put this function into viewRender in the config.

$scope.uiConfig = {
    calendar:{
        height: 450,
        editable: true,
        header:{
            left: 'title',
            center: '',
            right: 'today prev,next'
        },
        eventClick: $scope.alertOnEventClick,
        viewRender: function(view,element) {
            $scope.surgeries = SurgerySource.jsonp_query({
                start: view.visStart.getTime()/1000,
                end: view.visEnd.getTime()/1000
            });
            console.log($scope.surgeries);
        }
    }
};
$scope.surgeries = [];
$scope.eventSources = [$scope.surgeries];

I see the data logged when the calendar opens, but the events are not populated to the calendar.

So I either need to work out how to get the date range out of the calendar view, so I can use my hard coded method. Or I need to work out how to get the resource data out of the the promise(?) object and send it to $scope.eventSources

Foi útil?

Solução

I got this working by switching to $http instead of building a service. My new eventSource function looks like this

$scope.eventSource = function (start, end, callback) {
    var startDate = start.getTime()/1000;
    var endDate = end.getTime()/1000;
    var url = 'http://api.mydomain.com/app_dev.php/calendar.json?callback=JSON_CALLBACK&start='+startDate+'&end='+endDate;

    $http.jsonp(url)
        .then(function(response){
            callback(response.data);
        });
};

I also had to make sure my source was sending my timestamps rather than date strings.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top