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

5
src/storage_manager/index.js

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

6
src/storage_manager/model/LocalStorage.js

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

6
src/storage_manager/model/RemoteStorage.js

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

Loading…
Cancel
Save