(js) Fix creation of a Calendar

pull/101/head^2
Francis Lachapelle 2015-11-11 16:42:46 -05:00
parent 87fc7b39cc
commit b30f20e1b3
2 changed files with 50 additions and 2 deletions

View File

@ -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

View File

@ -63,7 +63,9 @@
owner: UserLogin
}
);
Calendar.$add(calendar);
calendar.$id().then(function() {
Calendar.$add(calendar);
});
});
}