MDL-63714 javascript: Add new core/pending module

This commit is contained in:
Andrew Nicols
2018-10-24 08:43:50 +08:00
parent 527f895e93
commit c85128260f
4 changed files with 75 additions and 19 deletions
+1
View File
@@ -0,0 +1 @@
define(["jquery"],function(a){var b=function(b){var c=a.Deferred();return b=b||{},M.util.js_pending(b),c.then(function(){return M.util.js_complete(b)})["catch"](),c};return b.prototype.constructor=b,b});
+1 -1
View File
File diff suppressed because one or more lines are too long
+52
View File
@@ -0,0 +1,52 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A helper to manage pendingJS checks.
*
* @module core/pending
* @package core
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.6
*/
define(['jquery'], function($) {
/**
* Request a new pendingPromise to be resolved.
*
* When the action you are performing is complete, simply call resolve on the returned Promise.
*
* @param {Object} pendingKey An optional key value to use
* @return {Promise}
*/
var request = function(pendingKey) {
var pendingPromise = $.Deferred();
pendingKey = pendingKey || {};
M.util.js_pending(pendingKey);
pendingPromise.then(function() {
return M.util.js_complete(pendingKey);
})
.catch();
return pendingPromise;
};
request.prototype.constructor = request;
return request;
});
+21 -18
View File
@@ -23,22 +23,25 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
define(['core/mustache',
'jquery',
'core/ajax',
'core/str',
'core/notification',
'core/url',
'core/config',
'core/localstorage',
'core/icon_system',
'core/event',
'core/yui',
'core/log',
'core/truncate',
'core/user_date'
],
function(mustache, $, ajax, str, notification, coreurl, config, storage, IconSystem, event, Y, Log, Truncate, UserDate) {
define([
'core/mustache',
'jquery',
'core/ajax',
'core/str',
'core/notification',
'core/url',
'core/config',
'core/localstorage',
'core/icon_system',
'core/event',
'core/yui',
'core/log',
'core/truncate',
'core/user_date',
'core/pending',
],
function(mustache, $, ajax, str, notification, coreurl, config, storage, IconSystem, event, Y, Log, Truncate, UserDate,
Pending) {
// Module variables.
/** @var {Number} uniqInstances Count of times this constructor has been called. */
@@ -509,7 +512,7 @@ define(['core/mustache',
this.currentThemeName = themeName;
var iconTemplate = iconSystem.getTemplateName();
M.util.js_pending('core/templates:doRender');
var pendingPromise = new Pending('core/templates:doRender');
return this.getTemplate(iconTemplate).then(function() {
this.addHelpers(context, themeName);
var result = mustache.render(templateSource, context, this.partialHelper.bind(this));
@@ -556,7 +559,7 @@ define(['core/mustache',
return $.Deferred().resolve(html, js).promise();
}.bind(this))
.then(function(html, js) {
M.util.js_complete('core/templates:doRender');
pendingPromise.resolve();
return $.Deferred().resolve(html, js).promise();
});
};