|
@@ -63,7 +63,7 @@ export default (options={}) => { |
|
|
reset() { |
|
|
reset() { |
|
|
let ambient_queries = [ |
|
|
let ambient_queries = [ |
|
|
...(_configs.selects ? _configs.selects : []), |
|
|
...(_configs.selects ? _configs.selects : []), |
|
|
...(_configs.wheres ? _configs.wheres.map(query => ({label: query.label, op: query.op, value: typeof query.value == 'function' ? query.value() : query.value})) : []), |
|
|
|
|
|
|
|
|
...(_configs.wheres ? _configs.wheres.map(where => ({label: where.label, op: where.op, value: typeof where.value == 'function' ? where.value() : where.value})) : []), |
|
|
...(_configs.order ? _configs.order : []) |
|
|
...(_configs.order ? _configs.order : []) |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
@@ -81,8 +81,6 @@ export default (options={}) => { |
|
|
|
|
|
|
|
|
// main methods |
|
|
// main methods |
|
|
select(offset=0, limit=Infinity) { |
|
|
select(offset=0, limit=Infinity) { |
|
|
_cache.data = _cache.data || [] |
|
|
|
|
|
|
|
|
|
|
|
// normalize limit |
|
|
// normalize limit |
|
|
if (limit == Infinity) |
|
|
if (limit == Infinity) |
|
|
limit = _cache.upstream_limit || _cache.count || Infinity |
|
|
limit = _cache.upstream_limit || _cache.count || Infinity |
|
@@ -172,44 +170,48 @@ export default (options={}) => { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
return _promise |
|
|
return _promise |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_model.reset() |
|
|
|
|
|
return _model |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const default1 = (args) => { |
|
|
|
|
|
// private model configs |
|
|
|
|
|
return { |
|
|
|
|
|
// model & model state |
|
|
|
|
|
fully_loaded: false, |
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
// load full model if privileged |
|
|
|
|
|
load_full() { |
|
|
|
|
|
this.reset() |
|
|
|
|
|
|
|
|
select_all() { |
|
|
|
|
|
// be lazy 1: if the data is presented, return the value immediately |
|
|
|
|
|
if (this.data().length == _cache.count && !this.data().includes(undefined)) |
|
|
|
|
|
return Promise.resolve(this.data()) |
|
|
|
|
|
|
|
|
if (this.fully_loaded) |
|
|
|
|
|
return Promise.resolve(this.list) |
|
|
|
|
|
|
|
|
// be lazy 2: if there is a promise, return it if ambient matches or |
|
|
|
|
|
// cancel it |
|
|
|
|
|
if (_promise != null) |
|
|
|
|
|
return _promise |
|
|
|
|
|
|
|
|
return api.request({ |
|
|
|
|
|
|
|
|
_promise = _configs.api.request({ |
|
|
method: 'POST', |
|
|
method: 'POST', |
|
|
url: '/rpc/query_all', |
|
|
|
|
|
|
|
|
url: '/rpc/select_all', |
|
|
body: { |
|
|
body: { |
|
|
endpoint: _configs.endpoint, |
|
|
endpoint: _configs.endpoint, |
|
|
selects: Array.from(_configs.selects, ([label, config]) => ({label: label, alias: config.alias})), |
|
|
|
|
|
queries: _configs.queries ? _configs.queries.map(query => ({label: query.label, op: query.op, value: typeof query.value == 'function' ? query.value() : query.value})) : [], |
|
|
|
|
|
|
|
|
selects: _configs.selects || [], |
|
|
|
|
|
wheres: _configs.wheres ? _configs.wheres.map(where => ({label: where.label, op: where.op, value: typeof where.value == 'function' ? where.value() : where.value})) : [], |
|
|
order: _configs.order |
|
|
order: _configs.order |
|
|
} |
|
|
} |
|
|
}).then(response => { |
|
|
}).then(response => { |
|
|
this.list = response.map(data => { |
|
|
|
|
|
_value_filters.forEach(([label, config]) => data[config.alias || label] = config.value_filter(data[config.alias || label])) |
|
|
|
|
|
|
|
|
_cache.count = response.length |
|
|
|
|
|
_cache.data = response.map(data => { |
|
|
|
|
|
// process values |
|
|
|
|
|
_configs.selects && _configs.selects |
|
|
|
|
|
.filter(select => select.processor) |
|
|
|
|
|
.forEach(select => data[select.alias || select.label] = select.processor(data[select.alias || select.label])) |
|
|
|
|
|
|
|
|
return data |
|
|
return data |
|
|
}) |
|
|
}) |
|
|
this.fully_loaded = true |
|
|
|
|
|
|
|
|
|
|
|
return this.list |
|
|
|
|
|
|
|
|
// clean state |
|
|
|
|
|
_promise = null |
|
|
|
|
|
this.reset() |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return _promise |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// initialize model |
|
|
|
|
|
_model.reset() |
|
|
|
|
|
return _model |
|
|
|
|
|
} |