MDL-57139 competencies: ensure promise best practices

This commit is contained in:
Dan Poltawski 2017-01-15 14:48:51 +00:00
parent 1fea12b0eb
commit 08c2360e4a
17 changed files with 103 additions and 102 deletions

View File

@ -131,6 +131,7 @@ define(['jquery',
html, html,
self._afterRender.bind(self) self._afterRender.bind(self)
); );
return;
}).fail(Notification.exception); }).fail(Notification.exception);
}; };
@ -156,6 +157,7 @@ define(['jquery',
return self._render().then(function(html) { return self._render().then(function(html) {
self._find('[data-region="action-selector"]').replaceWith(html); self._find('[data-region="action-selector"]').replaceWith(html);
self._afterRender(); self._afterRender();
return;
}); });
}; };

View File

@ -181,13 +181,13 @@ define(['jquery',
pagerender = 'tool_lp/plan_page'; pagerender = 'tool_lp/plan_page';
pageregion = 'plan-page'; pageregion = 'plan-page';
} }
ajax.call(requests)[requests.length - 1].then(function(context) { ajax.call(requests)[requests.length - 1].then(function(context) {
return templates.render(pagerender, context).done(function(html, js) { return templates.render(pagerender, context);
$('[data-region="' + pageregion + '"]').replaceWith(html); }).then(function(html, js) {
templates.runTemplateJS(js); $('[data-region="' + pageregion + '"]').replaceWith(html);
}); templates.runTemplateJS(js);
}, notification.exception); return;
}).catch(notification.exception);
}); });
} }

View File

@ -166,6 +166,7 @@ define(['jquery',
// We're done, let's trigger a change. // We're done, let's trigger a change.
self._templateLoaded = true; self._templateLoaded = true;
self._triggerChange(); self._triggerChange();
return;
}); });
}; };

View File

@ -430,12 +430,13 @@ define(['jquery',
var promises = ajax.call(calls); var promises = ajax.call(calls);
promises[calls.length - 1].then(function(context) { promises[calls.length - 1].then(function(context) {
return templates.render('tool_lp/related_competencies', context).done(function(html, js) { return templates.render('tool_lp/related_competencies', context);
$('[data-region="relatedcompetencies"]').replaceWith(html); }).then(function(html, js) {
templates.runTemplateJS(js); $('[data-region="relatedcompetencies"]').replaceWith(html);
updatedRelatedCompetencies(); templates.runTemplateJS(js);
}); updatedRelatedCompetencies();
}, notification.exception); return;
}).catch(notification.exception);
}); });
} }
@ -472,7 +473,8 @@ define(['jquery',
relatedTarget.ruleconfig = config.ruleconfig; relatedTarget.ruleconfig = config.ruleconfig;
renderCompetencySummary(relatedTarget); renderCompetencySummary(relatedTarget);
} }
}, notification.exception); return;
}).catch(notification.exception);
}; };
/** /**
@ -692,28 +694,27 @@ define(['jquery',
type: strs[1] type: strs[1]
}; };
} }
}).then(function() { return context;
return templates.render('tool_lp/competency_summary', context).then(function(html) { }).then(function(context) {
$('[data-region="competencyinfo"]').html(html); return templates.render('tool_lp/competency_summary', context);
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler); }).then(function(html) {
}); $('[data-region="competencyinfo"]').html(html);
}).then(function() { $('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
return templates.render('tool_lp/loading', {}); return templates.render('tool_lp/loading', {});
}).then(function(html, js) { }).then(function(html, js) {
templates.replaceNodeContents('[data-region="relatedcompetencies"]', html, js); templates.replaceNodeContents('[data-region="relatedcompetencies"]', html, js);
}).done(function() { return ajax.call([{
ajax.call([{
methodname: 'tool_lp_data_for_related_competencies_section', methodname: 'tool_lp_data_for_related_competencies_section',
args: {competencyid: competency.id}, args: {competencyid: competency.id}
done: function(context) { }])[0];
return templates.render('tool_lp/related_competencies', context).done(function(html, js) { }).then(function(context) {
$('[data-region="relatedcompetencies"]').replaceWith(html); return templates.render('tool_lp/related_competencies', context);
templates.runTemplateJS(js); }).then(function(html, js) {
updatedRelatedCompetencies(); $('[data-region="relatedcompetencies"]').replaceWith(html);
}); templates.runTemplateJS(js);
} updatedRelatedCompetencies();
}]); return;
}).fail(notification.exception); }).catch(notification.exception);
}; };
/** /**
@ -776,16 +777,17 @@ define(['jquery',
// Log Competency viewed event. // Log Competency viewed event.
triggerCompetencyViewedEvent(competency); triggerCompetencyViewedEvent(competency);
} }
strSelectedTaxonomy(level).then(function(str) { strSelectedTaxonomy(level).then(function(str) {
selectedTitle.text(str); selectedTitle.text(str);
}); return;
}).catch(notification.exception);
strAddTaxonomy(sublevel).then(function(str) { strAddTaxonomy(sublevel).then(function(str) {
btn.show() btn.show()
.find('[data-region="term"]') .find('[data-region="term"]')
.text(str); .text(str);
}); return;
}).catch(notification.exception);
// We handled this event so consume it. // We handled this event so consume it.
evt.preventDefault(); evt.preventDefault();

View File

@ -134,7 +134,7 @@ define(['jquery',
if (!self._singleFramework) { if (!self._singleFramework) {
self._find('[data-action="chooseframework"]').change(function(e) { self._find('[data-action="chooseframework"]').change(function(e) {
self._frameworkId = $(e.target).val(); self._frameworkId = $(e.target).val();
self._loadCompetencies().then(self._refresh.bind(self)); self._loadCompetencies().then(self._refresh.bind(self)).catch(Notification.exception);
}); });
} }
@ -203,15 +203,15 @@ define(['jquery',
*/ */
Picker.prototype.display = function() { Picker.prototype.display = function() {
var self = this; var self = this;
return self._render().then(function(html) { return $.when(Str.get_string('competencypicker', 'tool_lp'), self._render())
return Str.get_string('competencypicker', 'tool_lp').then(function(title) { .then(function(title, render) {
self._popup = new Dialogue( self._popup = new Dialogue(
title, title,
html, render[0],
self._afterRender.bind(self) self._afterRender.bind(self)
); );
}); return;
}).fail(Notification.exception); }).catch(Notification.exception);
}; };
/** /**
@ -388,6 +388,7 @@ define(['jquery',
return self._render().then(function(html) { return self._render().then(function(html) {
self._find('[data-region="competencylinktree"]').replaceWith(html); self._find('[data-region="competencylinktree"]').replaceWith(html);
self._afterRender(); self._afterRender();
return;
}); });
}; };

View File

@ -77,7 +77,8 @@ define(['jquery',
if (!self._singlePlan) { if (!self._singlePlan) {
self._find('[data-action="chooseplan"]').change(function(e) { self._find('[data-action="chooseplan"]').change(function(e) {
self._planId = $(e.target).val(); self._planId = $(e.target).val();
self._loadCompetencies().then(self._refresh.bind(self)); self._loadCompetencies().then(self._refresh.bind(self))
.catch(Notification.exception);
}); });
} }
}; };

View File

@ -165,14 +165,14 @@ define(['jquery',
if (!self._competency) { if (!self._competency) {
return false; return false;
} }
return self._render().then(function(html) { return $.when(Str.get_string('competencyrule', 'tool_lp'), self._render())
return Str.get_string('competencyrule', 'tool_lp').then(function(title) { .then(function(title, render) {
self._popup = new Dialogue( self._popup = new Dialogue(
title, title,
html, render[0],
self._afterRender.bind(self) self._afterRender.bind(self)
); );
}); return;
}).fail(Notification.exception); }).fail(Notification.exception);
}; };
@ -312,9 +312,9 @@ define(['jquery',
*/ */
RuleConfig.prototype._initOutcomes = function() { RuleConfig.prototype._initOutcomes = function() {
var self = this; var self = this;
return Outcomes.getAll().then(function(outcomes) { return Outcomes.getAll().then(function(outcomes) {
self._outcomesOption = outcomes; self._outcomesOption = outcomes;
return;
}); });
}; };
@ -328,11 +328,11 @@ define(['jquery',
RuleConfig.prototype._initRules = function() { RuleConfig.prototype._initRules = function() {
var self = this, var self = this,
promises = []; promises = [];
$.each(self._rules, function(index, rule) { $.each(self._rules, function(index, rule) {
var promise = rule.init().then(function() { var promise = rule.init().then(function() {
rule.setTargetCompetency(self._competency); rule.setTargetCompetency(self._competency);
rule.on('change', self._afterRuleConfigChange.bind(self)); rule.on('change', self._afterRuleConfigChange.bind(self));
return;
}, function() { }, function() {
// Upon failure remove the rule, and resolve the promise. // Upon failure remove the rule, and resolve the promise.
self._rules.splice(index, 1); self._rules.splice(index, 1);
@ -518,13 +518,13 @@ define(['jquery',
self._afterChange(); self._afterChange();
return; return;
} }
rule.injectTemplate(container).then(function() { rule.injectTemplate(container).then(function() {
container.show(); container.show();
}, function() { return;
container.empty().hide();
}).always(function() { }).always(function() {
self._afterChange(); self._afterChange();
}).catch(function() {
container.empty().hide();
}); });
}; };

View File

@ -76,6 +76,7 @@ define(['jquery',
}]); }]);
promise[0].then(function() { promise[0].then(function() {
parent.remove(); parent.remove();
return;
}).fail(Notification.exception); }).fail(Notification.exception);
} }
); );

View File

@ -51,7 +51,6 @@ define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {
includes: includes includes: includes
} }
}]); }]);
promise[0].then(function(results) { promise[0].then(function(results) {
var promises = [], var promises = [],
i = 0; i = 0;
@ -69,9 +68,10 @@ define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {
i++; i++;
}); });
success(results.cohorts); success(results.cohorts);
return;
}); });
}, failure); }).catch(failure);
} }
}; };

View File

@ -79,9 +79,10 @@ define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {
i++; i++;
}); });
success(results.users); success(results.users);
return;
}); });
}, failure); }).catch(failure);
} }
}; };

View File

@ -35,20 +35,17 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
* @return {Promise} * @return {Promise}
*/ */
list: function(contextId, options) { list: function(contextId, options) {
var promise, var args = {
args = {
context: { context: {
contextid: contextId contextid: contextId
} }
}; };
$.extend(args, typeof options === 'undefined' ? {} : options); $.extend(args, typeof options === 'undefined' ? {} : options);
promise = Ajax.call([{ return Ajax.call([{
methodname: 'core_competency_list_competency_frameworks', methodname: 'core_competency_list_competency_frameworks',
args: args args: args
}])[0]; }])[0];
return promise.fail(Notification.exception);
}, },
/** /**
@ -76,6 +73,7 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
* @param {String} query The query string. * @param {String} query The query string.
* @param {Function} callback A callback function receiving an array of results. * @param {Function} callback A callback function receiving an array of results.
*/ */
/* eslint-disable promise/no-callback-in-promise */
transport: function(selector, query, callback) { transport: function(selector, query, callback) {
var el = $(selector), var el = $(selector),
contextId = el.data('contextid'), contextId = el.data('contextid'),
@ -84,11 +82,10 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
if (!contextId) { if (!contextId) {
throw new Error('The attribute data-contextid is required on ' + selector); throw new Error('The attribute data-contextid is required on ' + selector);
} }
this.list(contextId, { this.list(contextId, {
query: query, query: query,
onlyvisible: onlyVisible, onlyvisible: onlyVisible,
}).then(callback); }).then(callback).catch(Notification.exception);
} }
}; };

View File

@ -81,6 +81,7 @@ define(['jquery', 'core/ajax', 'core/str', 'tool_lp/competencypicker', 'core/tem
Str.get_string('competencyframeworkroot', 'tool_lp').then(function(rootframework) { Str.get_string('competencyframeworkroot', 'tool_lp').then(function(rootframework) {
$(self.staticElementSelector).html(rootframework); $(self.staticElementSelector).html(rootframework);
$(self.inputHiddenSelector).val(data.competencyId); $(self.inputHiddenSelector).val(data.competencyId);
return;
}).fail(Notification.exception); }).fail(Notification.exception);
} }
}; };

View File

@ -110,15 +110,16 @@ define(['jquery',
* Callback to render the region template. * Callback to render the region template.
* *
* @param {Object} context The context for the template. * @param {Object} context The context for the template.
* @return {Promise}
*/ */
PlanActions.prototype._renderView = function(context) { PlanActions.prototype._renderView = function(context) {
var self = this; var self = this;
templates.render(self._template, context) return templates.render(self._template, context)
.done(function(newhtml, newjs) { .then(function(newhtml, newjs) {
$(self._region).replaceWith(newhtml); $(self._region).replaceWith(newhtml);
templates.runTemplateJS(newjs); templates.runTemplateJS(newjs);
}) return;
.fail(notification.exception); });
}; };
/** /**
@ -130,16 +131,15 @@ define(['jquery',
*/ */
PlanActions.prototype._callAndRefresh = function(calls, planData) { PlanActions.prototype._callAndRefresh = function(calls, planData) {
var self = this; var self = this;
calls.push({ calls.push({
methodname: self._contextMethod, methodname: self._contextMethod,
args: self._getContextArgs(planData) args: self._getContextArgs(planData)
}); });
// Apply all the promises, and refresh when the last one is resolved. // Apply all the promises, and refresh when the last one is resolved.
return $.when.apply($.when, ajax.call(calls)) return $.when.apply($, ajax.call(calls))
.then(function() { .then(function() {
self._renderView(arguments[arguments.length - 1]); return self._renderView(arguments[arguments.length - 1]);
}) })
.fail(notification.exception); .fail(notification.exception);
}; };

View File

@ -58,7 +58,6 @@ define(['jquery', 'core/notification', 'core/str', 'core/ajax', 'core/templates'
done: this._contextLoaded.bind(this), done: this._contextLoaded.bind(this),
fail: notification.exception fail: notification.exception
}]); }]);
// Log the user competency viewed in plan event. // Log the user competency viewed in plan event.
requests[0].then(function(result) { requests[0].then(function(result) {
var eventMethodName = 'core_competency_user_competency_viewed_in_plan'; var eventMethodName = 'core_competency_user_competency_viewed_in_plan';
@ -66,12 +65,11 @@ define(['jquery', 'core/notification', 'core/str', 'core/ajax', 'core/templates'
if (result.plan.iscompleted) { if (result.plan.iscompleted) {
eventMethodName = 'core_competency_user_competency_plan_viewed'; eventMethodName = 'core_competency_user_competency_plan_viewed';
} }
ajax.call([{ return ajax.call([{
methodname: eventMethodName, methodname: eventMethodName,
args: {competencyid: competencyId, userid: userId, planid: planId}, args: {competencyid: competencyId, userid: userId, planid: planId}
fail: notification.exception }])[0];
}]); }).catch(notification.exception);
});
}; };
/** /**

View File

@ -61,7 +61,7 @@ define(['jquery',
Ajax.call([call])[0].then(function() { Ajax.call([call])[0].then(function() {
this._trigger('review-request-cancelled', data); this._trigger('review-request-cancelled', data);
this._trigger('status-changed', data); this._trigger('status-changed', data);
}.bind(this), function() { }.bind(this)).catch(function() {
this._trigger('error-occured', data); this._trigger('error-occured', data);
}.bind(this)); }.bind(this));
}; };
@ -106,7 +106,7 @@ define(['jquery',
Ajax.call([call])[0].then(function() { Ajax.call([call])[0].then(function() {
this._trigger('review-requested', data); this._trigger('review-requested', data);
this._trigger('status-changed', data); this._trigger('status-changed', data);
}.bind(this), function() { }.bind(this)).catch(function() {
this._trigger('error-occured', data); this._trigger('error-occured', data);
}.bind(this)); }.bind(this));
}; };
@ -147,11 +147,10 @@ define(['jquery',
competencyid: data.competencyid competencyid: data.competencyid
} }
}; };
Ajax.call([call])[0].then(function() { Ajax.call([call])[0].then(function() {
this._trigger('review-started', data); this._trigger('review-started', data);
this._trigger('status-changed', data); this._trigger('status-changed', data);
}.bind(this), function() { }.bind(this)).catch(function() {
this._trigger('error-occured', data); this._trigger('error-occured', data);
}.bind(this)); }.bind(this));
}; };
@ -196,7 +195,7 @@ define(['jquery',
Ajax.call([call])[0].then(function() { Ajax.call([call])[0].then(function() {
this._trigger('review-stopped', data); this._trigger('review-stopped', data);
this._trigger('status-changed', data); this._trigger('status-changed', data);
}.bind(this), function() { }.bind(this)).catch(function() {
this._trigger('error-occured', data); this._trigger('error-occured', data);
}.bind(this)); }.bind(this));
}; };

View File

@ -98,14 +98,15 @@ define(['jquery',
* Callback to render the region template. * Callback to render the region template.
* *
* @param {Object} context The context for the template. * @param {Object} context The context for the template.
* @return {Promise}
*/ */
UserEvidenceActions.prototype._renderView = function(context) { UserEvidenceActions.prototype._renderView = function(context) {
var self = this; var self = this;
templates.render(self._template, context) return templates.render(self._template, context)
.done(function(newhtml, newjs) { .then(function(newhtml, newjs) {
templates.replaceNode($(self._region), newhtml, newjs); templates.replaceNode($(self._region), newhtml, newjs);
}) return;
.fail(notification.exception); });
}; };
/** /**
@ -117,7 +118,6 @@ define(['jquery',
*/ */
UserEvidenceActions.prototype._callAndRefresh = function(calls, evidenceData) { UserEvidenceActions.prototype._callAndRefresh = function(calls, evidenceData) {
var self = this; var self = this;
calls.push({ calls.push({
methodname: self._contextMethod, methodname: self._contextMethod,
args: self._getContextArgs(evidenceData) args: self._getContextArgs(evidenceData)
@ -126,7 +126,7 @@ define(['jquery',
// Apply all the promises, and refresh when the last one is resolved. // Apply all the promises, and refresh when the last one is resolved.
return $.when.apply($.when, ajax.call(calls)) return $.when.apply($.when, ajax.call(calls))
.then(function() { .then(function() {
self._renderView(arguments[arguments.length - 1]); return self._renderView(arguments[arguments.length - 1]);
}) })
.fail(notification.exception); .fail(notification.exception);
}; };

View File

@ -54,18 +54,15 @@ define(['jquery', 'core/notification', 'core/str', 'core/ajax', 'core/log', 'cor
var requests = ajax.call([{ var requests = ajax.call([{
methodname: 'tool_lp_data_for_user_competency_summary_in_course', methodname: 'tool_lp_data_for_user_competency_summary_in_course',
args: {userid: userId, competencyid: competencyId, courseid: courseId}, args: {userid: userId, competencyid: competencyId, courseid: courseId},
done: this._contextLoaded.bind(this), }, {
fail: notification.exception methodname: 'core_competency_user_competency_viewed_in_course',
args: {userid: userId, competencyid: competencyId, courseid: courseId},
}]); }]);
// Log the user competency viewed in course event. $.when.apply($, requests).then(function() {
requests[0].then(function() { this._contextLoaded.bind(this);
ajax.call([{ return;
methodname: 'core_competency_user_competency_viewed_in_course', }).catch(notification.exception);
args: {userid: userId, competencyid: competencyId, courseid: courseId},
fail: notification.exception
}]);
});
}; };
/** /**