From 1cef03c9ef17ae050f02c3d3ff5939ff8c9684c0 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Fri, 18 Oct 2024 08:47:31 +0200 Subject: [PATCH] [INTERNAL] Refactor getModel handling --- webapp/controller/App.controller.js | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/webapp/controller/App.controller.js b/webapp/controller/App.controller.js index a323ada0f..222deab71 100644 --- a/webapp/controller/App.controller.js +++ b/webapp/controller/App.controller.js @@ -19,12 +19,21 @@ sap.ui.define([ }), "view"); }, + /** + * Get the default model from the view + * + * @returns {sap.ui.model.json.JSONModel} The model containing the todo list, etc. + */ + getModel() { + return this.getView().getModel(); + }, + /** * Adds a new todo item to the bottom of the list. */ addTodo() { - const oModel = this.getView().getModel(); - const aTodos = oModel.getProperty("/todos").map((oTodo) => Object.assign({}, oTodo)); + const oModel = this.getModel(); + const aTodos = this.getTodos().map((oTodo) => Object.assign({}, oTodo)); aTodos.push({ title: oModel.getProperty("/newTodo"), @@ -39,17 +48,15 @@ sap.ui.define([ * Trigger removal of all completed items from the todo list. */ onClearCompleted() { - const oModel = this.getView().getModel(); - const aTodos = oModel.getProperty("/todos").map((oTodo) => Object.assign({}, oTodo)); + const aTodos = this.getTodos().map((oTodo) => Object.assign({}, oTodo)); this.removeCompletedTodos(aTodos); - oModel.setProperty("/todos", aTodos); + this.getModel().setProperty("/todos", aTodos); }, /** * Removes all completed items from the given todos. * * @param {object[]} aTodos - * @returns */ removeCompletedTodos(aTodos) { let i = aTodos.length; @@ -62,15 +69,21 @@ sap.ui.define([ }, /** - * Updates the number of items not yet completed + * Determines the todo list + * + * @returns {object[]} The todo list */ - onUpdateItemsLeftCount() { + getTodos(){ const oModel = this.getView().getModel(); - const aTodos = oModel.getProperty("/todos") || []; - - const iItemsLeft = aTodos.filter((oTodo) => oTodo.completed !== true).length; + return oModel.getProperty("/todos") || []; + }, - oModel.setProperty("/itemsLeftCount", iItemsLeft); + /** + * Updates the number of items not yet completed + */ + onUpdateItemsLeftCount() { + const iItemsLeft = this.getTodos().filter((oTodo) => oTodo.completed !== true).length; + this.getModel().setProperty("/itemsLeftCount", iItemsLeft); }, /**