From 7f9358fcc05b52e57d2c68257ad9585962972ba9 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 7 Mar 2012 11:25:18 +0800 Subject: [PATCH] MDL-31838: Allow caching static responses from repositories in file picker --- repository/filepicker.js | 18 +++++++++++++++--- repository/upload/lib.php | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/repository/filepicker.js b/repository/filepicker.js index 72d0a5668e82e..c9307e3a281a6 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -72,6 +72,7 @@ M.core_filepicker.init = function(Y, options) { Y.extend(FilePickerHelper, Y.Base, { api: M.cfg.wwwroot+'/repository/repository_ajax.php', + cached_responses: {}, initializer: function(options) { this.options = options; @@ -163,6 +164,11 @@ M.core_filepicker.init = function(Y, options) { if (data.msg) { scope.print_msg(data.msg, 'info'); } + // cache result if applicable + if (args.action != 'upload' && data.allowcaching) { + scope.cached_responses[params] = data; + } + // invoke callback args.callback(id,data,p); } } @@ -179,9 +185,15 @@ M.core_filepicker.init = function(Y, options) { if (args.form) { cfg.form = args.form; } - Y.io(api, cfg); - if (redraw) { - this.wait('load'); + // check if result of the same request has been already cached. If not, request it + // (never applicable in case of form submission and/or upload action): + if (!args.form && args.action != 'upload' && scope.cached_responses[params]) { + args.callback(null, scope.cached_responses[params], {scope: scope}) + } else { + Y.io(api, cfg); + if (redraw) { + this.wait('load'); + } } }, process_existing_file: function(data) { diff --git a/repository/upload/lib.php b/repository/upload/lib.php index 3dc6330eea11c..447fd0861e5c5 100644 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -223,6 +223,7 @@ public function get_listing() { $ret['list'] = array(); $ret['dynload'] = false; $ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form'); + $ret['allowcaching'] = true; // indicates that result of get_listing() can be cached in filepicker.js return $ret; }