diff --git a/src/util/make_model.js b/src/util/make_model.js index 0cce306..eb20761 100644 --- a/src/util/make_model.js +++ b/src/util/make_model.js @@ -48,8 +48,11 @@ export default (options={}) => { } // some random variables to make things work - let _xhr = null - let _promise = null + let _state = { + xhr: null, + promise: null, + loading: false + } // construct the model let _model = { @@ -74,7 +77,7 @@ export default (options={}) => { _cache.data = [] _cache.count = null _cache.upstream_limit = null - _xhr = null + _state.xhr = null //this.fully_loaded = false } }, @@ -91,8 +94,8 @@ export default (options={}) => { // be lazy 2: if there is a promise, return it if ambient matches or // cancel it - if (_promise != null) - return _promise + if (_state.promise != null) + return _state.promise // TODO // ambient = select + order + limit/offset @@ -100,13 +103,13 @@ export default (options={}) => { // current promise // now the hard work - _promise = _configs.api.request({ + _state.promise = _configs.api.request({ method: 'GET', url: _configs.endpoint, headers: _cache.count == null ? { Prefer: 'count=exact' } : {}, - config: xhr => _xhr = xhr, + config: xhr => _state.xhr = xhr, queries: [ // transform model state to postgest queries // selects @@ -135,7 +138,7 @@ export default (options={}) => { ] }).then(response => { // gather begin/end/count - let [_range, _count] = _xhr.getResponseHeader('content-range').split('/') + let [_range, _count] = _state.xhr.getResponseHeader('content-range').split('/') let [_begin, _end] = _range.split('-').map(v => ~~v) // update count if presented @@ -162,14 +165,14 @@ export default (options={}) => { console.warn('The response range is narrower than requested, probably due to an upstream hard limit.') // clean model state - _promise = null + _state.promise = null this.reset() // return data return _cache.data.slice(_begin, _end + 1) }) - return _promise + return _state.promise }, select_all() { @@ -179,10 +182,10 @@ export default (options={}) => { // be lazy 2: if there is a promise, return it if ambient matches or // cancel it - if (_promise != null) - return _promise + if (_state.promise != null) + return _state.promise - _promise = _configs.api.request({ + _state.promise = _configs.api.request({ method: 'POST', url: '/rpc/select_all', body: { @@ -203,11 +206,11 @@ export default (options={}) => { }) // clean state - _promise = null + _state.promise = null this.reset() }) - return _promise + return _state.promise }, export(options={}) {