1
0
Fork 0
mirror of synced 2024-07-05 06:20:55 +12:00

Add support for displaying linked record counts in List and RecordDetail

This commit is contained in:
Andrew Kingston 2020-10-06 18:01:23 +01:00
parent e15e86b9e1
commit 1fb5804d32
2 changed files with 36 additions and 1 deletions

View file

@ -8,6 +8,12 @@
let store = _bb.store
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() {
const FETCH_RECORDS_URL = `/api/views/all_${model}`
const response = await _bb.api.get(FETCH_RECORDS_URL)
@ -34,6 +40,14 @@
}
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, {
hydrate: false,
context: record,

View file

@ -4,7 +4,28 @@ export default async function fetchData(datasource) {
const { isModel, name } = datasource
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() {