UNPKG

ui5_easy_use

Version:

CLI tool for SAP ui5 and SAPUI5 projects to initialize apps, generate pages, insert form and table components, manage routing, and automate i18n bindings

144 lines (120 loc) 5.34 kB
sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/ui/model/Filter", "sap/ui/model/json/JSONModel" ], function (Controller, Filter, JSONModel) { "use strict"; return Controller.extend("${ez5.appName}.${ez5.packgName}.CRUDoData", { constructor: function (currentController, oModelName = "", isComponent = false) { Controller.apply(this, arguments); this._currentController = currentController; this.oModel = this._resolveModel(oModelName, isComponent); }, get_record: async function (endPoint, id = "", filter = {}) { const path = this._buildEntityPath(endPoint, id); const filters = this._buildFilters(filter); return await this._read(path, filters); }, post_record: async function (endPoint, payload) { return await this._create(`/${endPoint}`, payload); }, update_record: async function (endPoint, payload, id) { return await this._update(this._buildEntityPath(endPoint, id), payload); }, delete_record: async function (endPoint, id) { return await this._remove(this._buildEntityPath(endPoint, id)); }, get_record_set_model: async function (endPoint, id = "", filter = {}, modelName, callBack = function (results) { return results; }) { const response = await this.get_record(endPoint, id, filter); if (response && Array.isArray(response.results)) { const results = await callBack(this._stringifyRecordValues(response.results)); this._currentController.getView().setModel(new JSONModel(results), modelName); } return response; }, post_record_set_model: async function (endPoint, payload, modelName, callBack = function (results) { return results; }) { const response = await this.post_record(endPoint, payload); if (response) { await this.get_record_set_model(endPoint, "", {}, modelName, callBack); } return response; }, update_record_set_model: async function (endPoint, payload, modelName, callBack = function (results) { return results; }) { const response = await this.update_record(endPoint, payload, payload.Id); await this.get_record_set_model(endPoint, "", {}, modelName, callBack); return response; }, delete_record_set_model: async function (endPoint, id, modelName, callBack = function (results) { return results; }) { const response = await this.delete_record(endPoint, id); await this.get_record_set_model(endPoint, "", {}, modelName, callBack); return response; }, _resolveModel: function (modelName, isComponent) { if (!modelName) { return this._currentController.getOwnerComponent()?.getModel(); } return isComponent ? this._currentController.getModel(modelName) : this._currentController.getOwnerComponent()?.getModel(modelName); }, _buildEntityPath: function (endPoint, id) { return id ? `/${endPoint}('${id}')` : `/${endPoint}`; }, _buildFilters: function (filter = {}) { if (!filter.name || filter.value === undefined || filter.value === null) { return []; } return [new Filter(filter.name, "EQ", String(filter.value))]; }, _read: async function (path, filters) { return await this._runOData(function (resolve, reject) { this.oModel.read(path, { filters, success: resolve, error: reject }); }); }, _create: async function (path, payload) { return await this._runOData(function (resolve, reject) { this.oModel.create(path, payload, { success: resolve, error: reject }); }); }, _update: async function (path, payload) { return await this._runOData(function (resolve, reject) { this.oModel.update(path, payload, { success: resolve, error: reject }); }); }, _remove: async function (path) { return await this._runOData(function (resolve, reject) { this.oModel.remove(path, { success: resolve, error: reject }); }); }, _runOData: async function (executor) { try { return await new Promise(executor.bind(this)); } catch (error) { console.error(error); return null; } }, _stringifyRecordValues: function (records) { return records.map(function (record) { const nextRecord = { ...record }; Object.keys(nextRecord).forEach(function (key) { nextRecord[key] = String(nextRecord[key]); }); return nextRecord; }); } }); });