gristlabs_grist-core/test/fixtures/sites/fetchSelectedOptions/page.js
Alex Hall 4e67c679b2 (core) Options for plugin API functions which fetch data from the selected table or record
Summary:
Adds a new interface `FetchSelectedOptions` with three keys (including the preexisting `keepEncoded`) and adds/updates an optional `options: FetchSelectedOptions` to six related functions which fetch data from the selected table or record. The `keepEncoded` and `format` options have different default values for different methods for backwards compatibility, but otherwise the different methods now have much more similar behaviour. The new `includeColumns` option allows fetching all columns which was previously only possible using `docApi.fetchTable` (which wasn't always a great alternative) but this requires full access to avoid exposing more data than before and violating user expectations.

Eventually, similar options should be added to `docApi.fetchTable` to make the API even more consistent.

Discussion: https://grist.slack.com/archives/C0234CPPXPA/p1696510548994899

Test Plan: Added a new nbrowser test with a corresponding fixture site and document, showing how the functions have different default option values but are all configurable now.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D4077
2023-10-26 23:46:00 +02:00

102 lines
2.7 KiB
JavaScript

/* global document, grist, window */
function setup() {
const data = {
default: {},
options: {},
};
let showCount = 0;
function showData() {
showCount += 1;
if (showCount < 12) {
return;
}
document.getElementById('data').innerHTML = JSON.stringify(data, null, 2);
}
grist.onRecord(function (rec) {
data.default.onRecord = rec;
showData();
});
grist.onRecords(function (recs) {
data.default.onRecords = recs;
showData();
});
grist.fetchSelectedTable().then(function (table) {
data.default.fetchSelectedTable = table;
showData();
});
grist.fetchSelectedRecord(1).then(function (rec) {
data.default.fetchSelectedRecord = rec;
showData();
});
grist.viewApi.fetchSelectedTable().then(function (table) {
data.default.viewApiFetchSelectedTable = table;
showData();
});
grist.viewApi.fetchSelectedRecord(2).then(function (rec) {
data.default.viewApiFetchSelectedRecord = rec;
showData();
});
try {
grist.onRecord(function (rec) {
data.options.onRecord = rec;
showData();
}, {keepEncoded: true, includeColumns: 'normal', format: 'columns'});
} catch (e) {
data.options.onRecord = String(e);
showData();
}
try {
grist.onRecords(function (recs) {
data.options.onRecords = recs;
showData();
}, {keepEncoded: true, includeColumns: 'all', format: 'columns'});
} catch (e) {
data.options.onRecords = String(e);
showData();
}
grist.fetchSelectedTable(
{keepEncoded: true, includeColumns: 'all', format: 'rows'}
).then(function (table) {
data.options.fetchSelectedTable = table;
showData();
}).catch(function (err) {
data.options.fetchSelectedTable = String(err);
showData();
});
grist.fetchSelectedRecord(1,
{keepEncoded: true, includeColumns: 'normal', format: 'rows'}
).then(function (rec) {
data.options.fetchSelectedRecord = rec;
showData();
}).catch(function (err) {
data.options.fetchSelectedRecord = String(err);
showData();
});
grist.viewApi.fetchSelectedTable(
{keepEncoded: false, includeColumns: 'all', format: 'rows'}
).then(function (table) {
data.options.viewApiFetchSelectedTable = table;
showData();
}).catch(function (err) {
data.options.viewApiFetchSelectedTable = String(err);
showData();
});
grist.viewApi.fetchSelectedRecord(2,
{keepEncoded: false, includeColumns: 'normal', format: 'rows'}
).then(function (rec) {
data.options.viewApiFetchSelectedRecord = rec;
showData();
}).catch(function (err) {
data.options.viewApiFetchSelectedRecord = String(err);
showData();
});
grist.ready();
}
window.onload = setup;