Browse Source

Add callback function to load method

pull/187/head
Artur Arseniev 9 years ago
parent
commit
31e2931ccf
  1. 11
      src/editor/model/Editor.js
  2. 5
      src/storage_manager/index.js
  3. 6
      src/storage_manager/model/LocalStorage.js
  4. 6
      src/storage_manager/model/RemoteStorage.js

11
src/editor/model/Editor.js

@ -439,7 +439,7 @@ module.exports = Backbone.Model.extend({
* @private * @private
*/ */
load(clb) { load(clb) {
var result = this.getCacheLoad(1); var result = this.getCacheLoad(1, clb);
this.get('storables').forEach(m => { this.get('storables').forEach(m => {
m.load(result); m.load(result);
}); });
@ -449,10 +449,11 @@ module.exports = Backbone.Model.extend({
/** /**
* Returns cached load * Returns cached load
* @param {Boolean} force Force to reload * @param {Boolean} force Force to reload
* @param {Function} clb Callback function
* @return {Object} * @return {Object}
* @private * @private
*/ */
getCacheLoad(force) { getCacheLoad(force, clb) {
var f = force ? 1 : 0; var f = force ? 1 : 0;
if(this.cacheLoad && !f) if(this.cacheLoad && !f)
return this.cacheLoad; return this.cacheLoad;
@ -471,8 +472,10 @@ module.exports = Backbone.Model.extend({
}); });
}); });
this.cacheLoad = sm.load(load); this.cacheLoad = sm.load(load, (loaded) => {
this.trigger('storage:load', this.cacheLoad); clb && clb(loaded);
this.trigger('storage:load', loaded);
});
return this.cacheLoad; return this.cacheLoad;
}, },

5
src/storage_manager/index.js

@ -196,6 +196,7 @@ module.exports = () => {
/** /**
* Load resource from the current storage by keys * Load resource from the current storage by keys
* @param {string|Array<string>} keys Keys to load * @param {string|Array<string>} keys Keys to load
* @param {Function} clb Callback function
* @return {Object|null} Loaded resources * @return {Object|null} Loaded resources
* @example * @example
* var data = storageManager.load(['item1', 'item2']); * var data = storageManager.load(['item1', 'item2']);
@ -203,7 +204,7 @@ module.exports = () => {
* var data2 = storageManager.load('item1'); * var data2 = storageManager.load('item1');
* // data2 -> {item1: value1} * // data2 -> {item1: value1}
* */ * */
load(keys) { load(keys, clb) {
var st = this.get(this.getCurrent()); var st = this.get(this.getCurrent());
var keysF = []; var keysF = [];
var result = {}; var result = {};
@ -214,7 +215,7 @@ module.exports = () => {
for (var i = 0, len = keys.length; i < len; i++) for (var i = 0, len = keys.length; i < len; i++)
keysF.push(c.id + keys[i]); keysF.push(c.id + keys[i]);
var loaded = st ? st.load(keysF) : {}; var loaded = st ? st.load(keysF, clb) : {};
// Restore keys name // Restore keys name
for (var itemKey in loaded){ for (var itemKey in loaded){

6
src/storage_manager/model/LocalStorage.js

@ -23,7 +23,7 @@ module.exports = Backbone.Model.extend({
/** /**
* @private * @private
*/ */
load(keys) { load(keys, clb) {
this.checkStorageEnvironment(); this.checkStorageEnvironment();
var result = {}; var result = {};
@ -33,6 +33,10 @@ module.exports = Backbone.Model.extend({
result[keys[i]] = value; result[keys[i]] = value;
} }
if (typeof clb == 'function') {
clb(result);
}
return result; return result;
}, },

6
src/storage_manager/model/RemoteStorage.js

@ -42,7 +42,7 @@ module.exports = Backbone.Model.extend({
/** /**
* @private * @private
*/ */
load(keys) { load(keys, clb) {
var result = {}, var result = {},
fd = {}, fd = {},
params = this.get('params'); params = this.get('params');
@ -61,6 +61,10 @@ module.exports = Backbone.Model.extend({
method: 'GET', method: 'GET',
}).done(d => { }).done(d => {
result = d; result = d;
}).always((res) => {
if (typeof clb == 'function') {
clb(res);
}
}); });
return result; return result;
}, },

Loading…
Cancel
Save