Rewrite so that we add options as an object instead of a very long list of parameters

semantics-font
Svein-Tore Griff With 2015-04-09 14:00:00 +02:00
parent 5b55b78ded
commit 5b36e468ac
1 changed files with 26 additions and 19 deletions

View File

@ -143,7 +143,7 @@ H5P.init = function (target) {
var saveTimer, save = function () { var saveTimer, save = function () {
var state = instance.getCurrentState(); var state = instance.getCurrentState();
if (state !== undefined) { if (state !== undefined) {
H5P.setUserData(contentId, 'state', state, undefined, true, true); H5P.setUserData(contentId, 'state', state, {deleteOnChange: true});
} }
if (H5PIntegration.saveFreq) { if (H5PIntegration.saveFreq) {
// Continue autosave // Continue autosave
@ -1625,21 +1625,28 @@ H5P.createTitle = function(rawTitle, maxLength) {
* @param {number} contentId What content to get data for. * @param {number} contentId What content to get data for.
* @param {string} dataId Identifies the set of data for this content. * @param {string} dataId Identifies the set of data for this content.
* @param {object} data The data that is to be stored. * @param {object} data The data that is to be stored.
* @param {string} [subContentId] Identifies which data belongs to sub content. * @param {object} extras - object holding the following properties:
* @param {boolean} [preloaded=false] If the data should be loaded when content is loaded. * - {string} [subContentId] Identifies which data belongs to sub content.
* @param {boolean} [deleteOnChange=false] If the data should be invalidated when the content changes. * - {boolean} [preloaded=true] If the data should be loaded when content is loaded.
* @param {function} [errorCallback] Callback with error as parameters. * - {boolean} [deleteOnChange=false] If the data should be invalidated when the content changes.
* - {function} [errorCallback] Callback with error as parameters.
* - {boolean} [async=true]
*/ */
H5P.setUserData = function (contentId, dataId, data, subContentId, preloaded, deleteOnChange, errorCallback, async) { H5P.setUserData = function (contentId, dataId, data, extras) {
if (!subContentId) { var options = H5P.jQuery.extend(true, {}, {
subContentId = 0; // Default subContentId: 0,
} preloaded: true,
deleteOnChange: false,
async: true
}, extras);
try { try {
data = JSON.stringify(data); data = JSON.stringify(data);
} }
catch (err) { catch (err) {
errorCallback(err); if (options.errorCallback) {
options.errorCallback(err);
}
return; // Failed to serialize. return; // Failed to serialize.
} }
@ -1648,19 +1655,19 @@ H5P.createTitle = function(rawTitle, maxLength) {
content.contentUserData = {}; content.contentUserData = {};
} }
var preloadedData = content.contentUserData; var preloadedData = content.contentUserData;
if (preloadedData[subContentId] === undefined) { if (preloadedData[options.subContentId] === undefined) {
preloadedData[subContentId] = {}; preloadedData[options.subContentId] = {};
} }
if (data === preloadedData[subContentId][dataId]) { if (data === preloadedData[options.subContentId][dataId]) {
return; // No need to save this twice. return; // No need to save this twice.
} }
preloadedData[subContentId][dataId] = data; preloadedData[options.subContentId][dataId] = data;
contentUserDataAjax(contentId, dataId, subContentId, function (error, data) { contentUserDataAjax(contentId, dataId, options.subContentId, function (error, data) {
if (errorCallback && error) { if (options.errorCallback && error) {
errorCallback(error); options.errorCallback(error);
} }
}, data, preloaded, deleteOnChange, async); }, data, options.preloaded, options.deleteOnChange, options.async);
}; };
/** /**
@ -1703,7 +1710,7 @@ H5P.createTitle = function(rawTitle, maxLength) {
var state = instance.getCurrentState(); var state = instance.getCurrentState();
if (state !== undefined) { if (state !== undefined) {
// Async is not used to prevent the request from being cancelled. // Async is not used to prevent the request from being cancelled.
H5P.setUserData(instance.contentId, 'state', state, undefined, true, true, undefined, false); H5P.setUserData(instance.contentId, 'state', state, {deleteOnChange: true, async: false});
} }
} }