Skip to content

Commit

Permalink
[INTERNAL] Refactor getModel handling
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Oct 18, 2024
1 parent 1356eeb commit 1cef03c
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions webapp/controller/App.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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;
Expand All @@ -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);
},

/**
Expand Down

0 comments on commit 1cef03c

Please sign in to comment.