javascript - Angular ui-calendar events function called twice -
i developing project angularjs , using angular-ui ui-calendar. in code initialize eventsources model ui-calendar empty array ([]) , set ui-config "events parameter" custom function. function makes $http request , calls callback function given events function.
however, found out when load page or change month viewed left or right buttons, events function called twice. how can solve that?
here code:
function calendarctrl($scope, data){ var date = new date(); var d = date.getdate(); var m = date.getmonth(); var y = date.getfullyear(); // stores events var events = []; /* config object */ $scope.uiconfig = { calendar:{ height: 450, editable: true, header: { left: 'prev,next', center: 'title', right: 'month,agendaweek,agendaday' }, buttontext: { month: "ay", week: "hafta", day: "gün", list: "ajanda" }, alldaytext: "tüm gün", eventlimittext: "daha fazla", firstday: 1, timeformat: 'h:mm', axisformat: 'h:mm', lazyfetching: true, // here listen calendar change events events: getevents } }; // angular ui-calendar element $scope.eventsources = []; // calendar event handlers function getevents(from, to, callback){ console.log(from); // test purposes // request data server , when comes, call callback update calendar data.calendar.get(moment(from).unix(), moment(to).unix()).then(function(events){ angular.foreach(events, function(event){ event.start = moment.unix(event.start); if(event.end){ event.end = moment.unix(event.end); } else{ delete event.end; } var d = event.start; if(d.hour() == 0 && d.minute() == 0 && d.second() == 0){ event.allday = true; } if(event.important){ event.classname = "important-event"; } event.editable = true; }); callback(events); }); }
i have solution.
instead of registering event handler $scope.uiconfig.calendar.events, register same function $scope.eventsources array. that:
$scope.eventsources = [getevents];
whenever view changes , calendar needs data @ $scope.eventsources elements. , if element function called , results shown in calendar.
Comments
Post a Comment