From c1c023448155cee0e5eaca0d0664b6ce078ba4ff Mon Sep 17 00:00:00 2001 From: Josh Kurz Date: Tue, 9 Apr 2013 21:53:54 -0400 Subject: [PATCH] dynamically add calendar object to parent scope #10 --- demo/calendarDemo.js | 7 ++++++- demo/index.html | 14 +++++++++++--- src/calendar.js | 12 +++++++++--- test/calendar.spec.js | 14 +++----------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/demo/calendarDemo.js b/demo/calendarDemo.js index e0e8342..5b45c85 100644 --- a/demo/calendarDemo.js +++ b/demo/calendarDemo.js @@ -4,6 +4,7 @@ angular.module('calendarDemoApp', ['ui.calendar']); function CalendarCtrl($scope) { + $scope.myCalendar = ''; var date = new Date(); var d = date.getDate(); var m = date.getMonth(); @@ -32,7 +33,7 @@ function CalendarCtrl($scope) { callback(events); }; /* alert on eventClick */ - $scope.addEventOnClick = function( date, allDay, jsEvent, view ){ + $scope.alertEventOnClick = function( date, allDay, jsEvent, view ){ $scope.$apply(function(){ $scope.alertMessage = ('Day Clicked ' + date); }); @@ -75,6 +76,10 @@ function CalendarCtrl($scope) { $scope.remove = function(index) { $scope.events.splice(index,1); }; + /* Change View */ + $scope.changeView = function(view) { + $scope.myCalendar.fullCalendar('changeView',view); + }; /* config object */ $scope.uiConfig = { calendar:{ diff --git a/demo/index.html b/demo/index.html index 20fc5bd..3479bf8 100644 --- a/demo/index.html +++ b/demo/index.html @@ -77,12 +77,14 @@

Why?

{{alertMessage}}

-

You can drag events below

+ + +
-
+
@@ -93,6 +95,7 @@

How?

angular.module('calendarDemoApp', ['ui.calendar']); function CalendarCtrl($scope) { + $scope.myCalendar = ''; var date = new Date(); var d = date.getDate(); var m = date.getMonth(); @@ -121,7 +124,7 @@

How?

callback(events); }; /* alert on eventClick */ - $scope.addEventOnClick = function( date, allDay, jsEvent, view ){ + $scope.alertEventOnClick = function( date, allDay, jsEvent, view ){ $scope.$apply(function(){ $scope.alertMessage = ('Day Clicked ' + date); }); @@ -164,6 +167,10 @@

How?

$scope.remove = function(index) { $scope.events.splice(index,1); }; + /* Change View */ + $scope.changeView = function(view) { + $scope.myCalendar.fullCalendar('changeView',view); + }; /* config object */ $scope.uiConfig = { calendar:{ @@ -182,6 +189,7 @@

How?

/* event sources array*/ $scope.eventSources = [$scope.events, $scope.eventSource, $scope.eventsF]; } +} diff --git a/src/calendar.js b/src/calendar.js index 52871f4..619e910 100644 --- a/src/calendar.js +++ b/src/calendar.js @@ -17,9 +17,15 @@ angular.module('ui.calendar', []) require: 'ngModel', scope: true, restrict: 'A', - link: function(scope, elm, attrs) { + link: function(scope, elm, attrs, parent) { var sources = scope.$eval(attrs.ngModel); - var calendar = elm.html(''); + var calendar; + if(!attrs.calendar){ + calendar = elm.html(''); + } + else{ + calendar = angular.element(elm.parent()).scope()[attrs.calendar] = elm.html(''); + } var eventsFingerprint = function() { var fpn = ""; angular.forEach(sources, function(events) { @@ -112,4 +118,4 @@ angular.module('ui.calendar', []) }); } }; -}]); +}]); \ No newline at end of file diff --git a/test/calendar.spec.js b/test/calendar.spec.js index 158f70c..75cab69 100644 --- a/test/calendar.spec.js +++ b/test/calendar.spec.js @@ -29,7 +29,7 @@ describe('uiCalendar', function () { {title: 'Long Event 2',start: new Date(y, m, d - 5),end: new Date(y, m, d - 2)}, {id: 998,title: 'Repeating Event 2',start: new Date(y, m, d - 3, 16, 0),allDay: false}, {id: 998,title: 'Repeating Event 2',start: new Date(y, m, d + 4, 16, 0),allDay: true}]; - //array to test equalsTracker with + //array to test equals case with scope.events3 = [ {title: 'All Day Event 3',start: new Date(y, m, 1),url: 'http://www.atlantacarlocksmith.com'}, {title: 'Long Event 3',start: new Date(y, m, d - 5),end: new Date(y, m, d - 2)}, @@ -39,11 +39,6 @@ describe('uiCalendar', function () { // create an array of events, to pass into the directive. scope.events4 = [{title: 'All Day Event 3',start: new Date(y, m, 1),url: 'http://www.yoyoyo.com'}]; - //equalsTracker to force the calendar to update on rare occasions - //ie... an event source is replacing another and has the same length as the prior eventSource - //or... replacing an eventSource that is an object with another eventSource - scope.equalsTracker = 0; - //event Sources array scope.eventSources = [scope.events,scope.events2]; //End of Events Array @@ -186,7 +181,7 @@ describe('uiCalendar', function () { /* Test to see if calendar is updating when an eventSource replaces another with an equal length. */ it('should update the calendar if an eventSource has same length as prior eventSource', function () { spyOn($.fn, 'fullCalendar'); - $compile('
')(scope); + $compile('
')(scope); var clientEventSources = $.fn.fullCalendar.mostRecentCall.args[0].eventSources; var clientEventsLength = $.fn.fullCalendar.mostRecentCall.args[0].eventSources[0].length; expect(clientEventsLength).toEqual(4); @@ -194,9 +189,6 @@ describe('uiCalendar', function () { expect(clientEventSources[1][0].title).toEqual('All Day Event 2'); //replace source with one that has the same length scope.eventSources.splice(1,1,scope.events3); - //must update the equalsTracker as we would detect that the length is equal from controller - scope.equalsTracker++; - //eventSources should update inside the calendar since we incremented the equalsTracker clientEventSources = $.fn.fullCalendar.mostRecentCall.args[0].eventSources; expect(clientEventSources.length).toEqual(2); expect(clientEventSources[1][0].title).toEqual('All Day Event 3'); @@ -205,7 +197,7 @@ describe('uiCalendar', function () { clientEventsLength = $.fn.fullCalendar.mostRecentCall.args[0].eventSources[0].length; expect(clientEventsLength).toEqual(3); - }); + }); });