diff --git a/src/editor/index.js b/src/editor/index.js index 5a42c953b..428794440 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -495,6 +495,17 @@ export default (config = {}) => { return em.load(clb); }, + /** + * Load data from the JSON data object + * @param {Object} data Data to load + * @return {Object} Loaded object + * @example + * editor.loadData({ pages: [...], styles: [...], ... }) + */ + loadData(data) { + return em.loadData(data); + }, + /** * Returns container element. The one which was indicated as 'container' * on init method diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index e12f3298f..6385faf73 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -623,14 +623,23 @@ export default Backbone.Model.extend({ */ load(clb = null) { this.getCacheLoad(1, res => { - this.get('storables').forEach(module => { - module.load(res); - module.postLoad && module.postLoad(this); - }); + this.loadData(res); clb && clb(res); }); }, + loadData(data = {}) { + const sm = this.get('StorageManager'); + const result = sm.__clearKeys(data); + + this.get('storables').forEach(module => { + module.load(result); + module.postLoad && module.postLoad(this); + }); + + return result; + }, + /** * Returns cached load * @param {Boolean} force Force to reload diff --git a/src/storage_manager/index.js b/src/storage_manager/index.js index f325b9cac..07027b95e 100644 --- a/src/storage_manager/index.js +++ b/src/storage_manager/index.js @@ -239,9 +239,9 @@ export default () => { * }); * */ load(keys, clb) { - var st = this.get(this.getCurrent()); - var keysF = []; - var result = {}; + const st = this.get(this.getCurrent()); + const keysF = []; + let result = {}; if (typeof keys === 'string') keys = [keys]; this.onStart('load', keys); @@ -254,13 +254,7 @@ export default () => { st.load( keysF, res => { - // Restore keys name - var reg = new RegExp('^' + c.id + ''); - for (var itemKey in res) { - var itemKeyR = itemKey.replace(reg, ''); - result[itemKeyR] = res[itemKey]; - } - + result = this.__clearKeys(res); this.onAfter('load', result); clb && clb(result); this.onEnd('load', result); @@ -275,6 +269,24 @@ export default () => { } }, + /** + * Restore key names + * @param {Object} data + * @returns {Object} + * @private + */ + __clearKeys(data = {}) { + const result = {}; + const reg = new RegExp('^' + c.id + ''); + + for (let itemKey in data) { + const itemKeyR = itemKey.replace(reg, ''); + result[itemKeyR] = data[itemKey]; + } + + return result; + }, + /** * Load default storages * @return {this}