Add support for displaying linked record counts in List and RecordDetail
This commit is contained in:
parent
e15e86b9e1
commit
1fb5804d32
|
@ -8,6 +8,12 @@
|
||||||
let store = _bb.store
|
let store = _bb.store
|
||||||
let target
|
let target
|
||||||
|
|
||||||
|
async function fetchModel(id) {
|
||||||
|
const FETCH_MODEL_URL = `/api/models/${id}`
|
||||||
|
const response = await _bb.api.get(FETCH_MODEL_URL)
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchFirstRecord() {
|
async function fetchFirstRecord() {
|
||||||
const FETCH_RECORDS_URL = `/api/views/all_${model}`
|
const FETCH_RECORDS_URL = `/api/views/all_${model}`
|
||||||
const response = await _bb.api.get(FETCH_RECORDS_URL)
|
const response = await _bb.api.get(FETCH_RECORDS_URL)
|
||||||
|
@ -34,6 +40,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (record) {
|
if (record) {
|
||||||
|
// Fetch model schema so we can check for linked records
|
||||||
|
const model = await fetchModel(record.modelId)
|
||||||
|
for (let key of Object.keys(model.schema)) {
|
||||||
|
if (model.schema[key].type === "link") {
|
||||||
|
record[key] = Array.isArray(record[key]) ? record[key].length : 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_bb.attachChildren(target, {
|
_bb.attachChildren(target, {
|
||||||
hydrate: false,
|
hydrate: false,
|
||||||
context: record,
|
context: record,
|
||||||
|
|
|
@ -4,7 +4,28 @@ export default async function fetchData(datasource) {
|
||||||
const { isModel, name } = datasource
|
const { isModel, name } = datasource
|
||||||
|
|
||||||
if (name) {
|
if (name) {
|
||||||
return isModel ? await fetchModelData() : await fetchViewData()
|
const records = isModel ? await fetchModelData() : await fetchViewData()
|
||||||
|
|
||||||
|
// Fetch model schema so we can check for linked records
|
||||||
|
if (records && records.length) {
|
||||||
|
const model = await fetchModel(records[0].modelId)
|
||||||
|
const keys = Object.keys(model.schema)
|
||||||
|
records.forEach(record => {
|
||||||
|
for (let key of keys) {
|
||||||
|
if (model.schema[key].type === "link") {
|
||||||
|
record[key] = Array.isArray(record[key]) ? record[key].length : 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return records
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchModel(id) {
|
||||||
|
const FETCH_MODEL_URL = `/api/models/${id}`
|
||||||
|
const response = await api.get(FETCH_MODEL_URL)
|
||||||
|
return await response.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchModelData() {
|
async function fetchModelData() {
|
||||||
|
|
Loading…
Reference in New Issue