Просмотр исходного кода

make_model: re-enable select_all

master
Bing Sun 5 лет назад
Родитель
Сommit
47cd6a182d
Подписано: sunb Идентификатор GPG ключа: F7795F8C590626AB
1 измененных файлов: 31 добавлений и 29 удалений
  1. +31
    -29
      src/util/make_model.js

+ 31
- 29
src/util/make_model.js Просмотреть файл

@@ -63,7 +63,7 @@ export default (options={}) => {
reset() {
let ambient_queries = [
...(_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 : [])
]

@@ -81,8 +81,6 @@ export default (options={}) => {

// main methods
select(offset=0, limit=Infinity) {
_cache.data = _cache.data || []

// normalize limit
if (limit == Infinity)
limit = _cache.upstream_limit || _cache.count || Infinity
@@ -172,44 +170,48 @@ export default (options={}) => {
})

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',
url: '/rpc/query_all',
url: '/rpc/select_all',
body: {
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
}
}).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
})
this.fully_loaded = true

return this.list
// clean state
_promise = null
this.reset()
})
}

return _promise
}
}

// initialize model
_model.reset()
return _model
}

Загрузка…
Отмена
Сохранить