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

View File

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

View File

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

View File

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

View File

@ -134,7 +134,7 @@ define(['jquery',
if (!self._singleFramework) {
self._find('[data-action="chooseframework"]').change(function(e) {
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() {
var self = this;
return self._render().then(function(html) {
return Str.get_string('competencypicker', 'tool_lp').then(function(title) {
self._popup = new Dialogue(
title,
html,
self._afterRender.bind(self)
);
});
}).fail(Notification.exception);
return $.when(Str.get_string('competencypicker', 'tool_lp'), self._render())
.then(function(title, render) {
self._popup = new Dialogue(
title,
render[0],
self._afterRender.bind(self)
);
return;
}).catch(Notification.exception);
};
/**
@ -388,6 +388,7 @@ define(['jquery',
return self._render().then(function(html) {
self._find('[data-region="competencylinktree"]').replaceWith(html);
self._afterRender();
return;
});
};

View File

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

View File

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

View File

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

View File

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

View File

@ -35,20 +35,17 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
* @return {Promise}
*/
list: function(contextId, options) {
var promise,
args = {
var args = {
context: {
contextid: contextId
}
};
$.extend(args, typeof options === 'undefined' ? {} : options);
promise = Ajax.call([{
return Ajax.call([{
methodname: 'core_competency_list_competency_frameworks',
args: args
}])[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 {Function} callback A callback function receiving an array of results.
*/
/* eslint-disable promise/no-callback-in-promise */
transport: function(selector, query, callback) {
var el = $(selector),
contextId = el.data('contextid'),
@ -84,11 +82,10 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
if (!contextId) {
throw new Error('The attribute data-contextid is required on ' + selector);
}
this.list(contextId, {
query: query,
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) {
$(self.staticElementSelector).html(rootframework);
$(self.inputHiddenSelector).val(data.competencyId);
return;
}).fail(Notification.exception);
}
};

View File

@ -110,15 +110,16 @@ define(['jquery',
* Callback to render the region template.
*
* @param {Object} context The context for the template.
* @return {Promise}
*/
PlanActions.prototype._renderView = function(context) {
var self = this;
templates.render(self._template, context)
.done(function(newhtml, newjs) {
return templates.render(self._template, context)
.then(function(newhtml, newjs) {
$(self._region).replaceWith(newhtml);
templates.runTemplateJS(newjs);
})
.fail(notification.exception);
return;
});
};
/**
@ -130,16 +131,15 @@ define(['jquery',
*/
PlanActions.prototype._callAndRefresh = function(calls, planData) {
var self = this;
calls.push({
methodname: self._contextMethod,
args: self._getContextArgs(planData)
});
// 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() {
self._renderView(arguments[arguments.length - 1]);
return self._renderView(arguments[arguments.length - 1]);
})
.fail(notification.exception);
};

View File

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

View File

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

View File

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

View File

@ -54,18 +54,15 @@ define(['jquery', 'core/notification', 'core/str', 'core/ajax', 'core/log', 'cor
var requests = ajax.call([{
methodname: 'tool_lp_data_for_user_competency_summary_in_course',
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.
requests[0].then(function() {
ajax.call([{
methodname: 'core_competency_user_competency_viewed_in_course',
args: {userid: userId, competencyid: competencyId, courseid: courseId},
fail: notification.exception
}]);
});
$.when.apply($, requests).then(function() {
this._contextLoaded.bind(this);
return;
}).catch(notification.exception);
};
/**