diff --git a/UI/WebServerResources/js/Scheduler/Calendar.service.js b/UI/WebServerResources/js/Scheduler/Calendar.service.js index 1d8293baa..9d8244713 100644 --- a/UI/WebServerResources/js/Scheduler/Calendar.service.js +++ b/UI/WebServerResources/js/Scheduler/Calendar.service.js @@ -14,7 +14,7 @@ if (this.name && !this.id) { // Create a new calendar on the server var newCalendarData = Calendar.$$resource.create('createFolder', this.name); - angular.extend(this, newCalendarData); + this.$unwrap(newCalendarData); } if (this.id) { this.$acl = new Calendar.$$Acl('Calendar/' + this.id); @@ -239,6 +239,7 @@ * @param {object} data - attributes of calendar */ Calendar.prototype.init = function(data) { + this.color = this.color || '#AAAAAA'; angular.extend(this, data); // Add 'isOwned' and 'isSubscription' attributes based on active user (TODO: add it server-side?) this.isOwned = Calendar.activeUser.isSuperUser || this.owner == Calendar.activeUser.login; @@ -249,6 +250,25 @@ } }; + /** + * @function $id + * @memberof Calendar.prototype + * @desc Resolve the calendar id. + * @returns a promise of the calendar id + */ + Calendar.prototype.$id = function() { + if (this.id) { + // Object already unwrapped + return Calendar.$q.when(this.id); + } + else { + // Wait until object is unwrapped + return this.$futureCalendarData.then(function(calendar) { + return calendar.id; + }); + } + }; + /** * @function getClassName * @memberof Calendar.prototype @@ -383,6 +403,32 @@ return Calendar.$Component.$find(this.id, componentId, recurrenceId); }; + /** + * @function $unwrap + * @memberof Calendar.prototype + * @desc Unwrap a promise + * @param {promise} futureCalendarData - a promise of the Calendar's data + */ + Calendar.prototype.$unwrap = function(futureCalendarData) { + var _this = this; + + // Expose and resolve the promise + this.$futureCalendarData = futureCalendarData.then(function(data) { + return Calendar.$timeout(function() { + // Extend Calendar instance with received data + _this.init(data); + return _this; + }); + }, function(data) { + _this.isError = true; + if (angular.isObject(data)) { + Calendar.$timeout(function() { + angular.extend(_this, data); + }); + } + }); + }; + /** * @function $omit * @memberof Calendar.prototype diff --git a/UI/WebServerResources/js/Scheduler/CalendarsController.js b/UI/WebServerResources/js/Scheduler/CalendarsController.js index 5bc3dd27f..77ef4e6ad 100644 --- a/UI/WebServerResources/js/Scheduler/CalendarsController.js +++ b/UI/WebServerResources/js/Scheduler/CalendarsController.js @@ -63,7 +63,9 @@ owner: UserLogin } ); - Calendar.$add(calendar); + calendar.$id().then(function() { + Calendar.$add(calendar); + }); }); }