service-utilities
Version:
Utility Package for FIORI UI5
130 lines (120 loc) • 4.56 kB
JavaScript
/**
* Model Data creator that simplifies the creation of View Models.
*
* If autoRefresh is disabled, setting and getting property/s should be done using the set, setProperties, get, and getProperties, respectively.
* If autoRefresh is enabled, directly modifying the value property will update the model data itself.
* Usage of set, setProperties, get, and getProperties even when autoRefresh is enabled still works.
*
* Note: autoRefresh continuously uses setTimeout so if you're having poor performance, it is recommended to turn this off.
*
* @module FactoryModelResource
* @author jpanti
* @version 1.0.0
* @created 2025-08-01
* @lastModified 2025-08-01
* @license ISC
*/
sap.ui.define(
["./ServiceModelResource"],
(ServiceModelResource) => {
"use strict";
class FactoryModelResource {
// Private Properties ===============================
#cacheOData;
#serviceModelResource;
// ==================================================
// Public Properties ================================
modelname = "";
value = {};
// ==================================================
// Optional Properties ==============================
setToGlobal;
autoRefresh;
autoRefreshInterval;
// ==================================================
// Initialization ===================================
constructor(
oController,
modelname,
oData,
{
setToGlobal = false,
autoRefresh = false,
autoRefreshInterval = 10,
} = {}
) {
this.modelname = modelname;
this.value = oData;
this.value.modelname = modelname;
this.setToGlobal = setToGlobal;
this.autoRefresh = autoRefresh;
this.autoRefreshInterval = autoRefreshInterval;
this.#serviceModelResource = new ServiceModelResource(oController);
this.#serviceModelResource.setModelSchema(this.value, setToGlobal);
this.refresh();
this.#runAutoRefresh();
}
// ==================================================
// Controller Methods ===============================
getController = () => this.#serviceModelResource.getController();
// ==================================================
// Watcher Methods ==================================
#runAutoRefresh() {
if (this.autoRefresh) {
setTimeout(() => {
if (this.#cacheOData !== this.value) this.refresh();
this.#runAutoRefresh();
}, this.autoRefreshInterval);
}
}
watchProperties(callback, ...keys) {
const oModel = this.getModel();
oModel.attachPropertyChange((oEvent) => {
const key = oEvent.getParameter("path").substring(1);
const value = oEvent.getParameter("value");
if (keys.length === 0 || keys.includes(key)) callback(key, value);
});
}
// ==================================================
// Property Modification Methods ====================
get = (key) => this.value[key] ?? undefined;
getProperties(keys, { includeNewProperties = false } = {}) {
const object = {};
keys.forEach((key) => {
if (this.value.hasOwnProperty(key) || includeNewProperties)
object[key] = this.get(key);
});
return object;
}
set(key, value, { includeNewProperties = false, doRefresh = true } = {}) {
if (this.value.hasOwnProperty(key) || includeNewProperties)
this.value[key] = value;
if (doRefresh) this.refresh();
}
setProperties(oData, { includeNewProperties = false } = {}) {
Object.entries(oData).forEach(([key, value]) =>
this.set(key, value, { includeNewProperties, doRefresh: false })
);
this.refresh();
}
refresh = () => {
this.#serviceModelResource.updateModel(
this.modelname,
this.setToGlobal
);
this.#cacheOData = this.getModelResource;
};
// ==================================================
// Model Data Methods ===============================
getModel = () =>
this.#serviceModelResource.getModel(this.modelname, this.setToGlobal);
getData = () =>
this.#serviceModelResource.getModelResource(
this.modelname,
this.setToGlobal
);
// ==================================================
}
return FactoryModelResource;
}
);