Issue with data synchronization in SAPUI5 custom control's two-way binding not resolved

As I work on creating a custom Control and binding data to it, I encounter an issue where upon clicking a button, the data in the Model is changed but not reflected in the DOM. Being new to SAP-UI5, I am struggling to identify the root cause of this problem. Any guidance or support would be greatly appreciated.

Here is the code snippet I am currently using:

    var data = { person : [{firstName:"Bruce", lastName:"Wayne"}] };
    //Setting up the Model
    var model = new sap.ui.model.json.JSONModel(data);
    sap.ui.getCore().setModel(model);

    //Defining Renderer
    jQuery.sap.declare("PersonRenderer");
    PersonRenderer={render : function (renderManager, control){
                    renderManager.write("<ul style = 'margin:20px 50px;'");
                    renderManager.writeControlData(control);
                    renderManager.write(">");

                    var items = control.getModel().getData().person;
                    for(var i=0, l=items.length; i<l; i++){
                        renderManager.write("<li style='padding-top : 20px; font-size : 16px; border : 1px solid #cecece; color : #006699; font-weight : bold'> ");
                        renderManager.write(items[i].firstName);
                        renderManager.write("&nbsp;");
                        renderManager.write(items[i].lastName);
                        renderManager.write("</li>");
                    }
                    renderManager.write("</ul");
                } }

     //Creating custom Element for binding. 
     //**Is it possible to skip this and utilize existing controls like *TextField***???//

     sap.ui.core.Element.extend("PersonItems",{
        metadata : {
            properties : {
                "first" : {type : "string"},
                "last" : {type : "string"}
            }
        } });

      //Defining custom control
      sap.ui.core.Control.extend("Person",{
        metadata : {
            properties : {
                aggregations : {
                    "items" : {type : "PersonItems", multiple : true, singularName : "Name_Singular"}
                },
                defaultAggregation : "items"
            },
            renderer :  PersonRenderer
        } });   

    //creating custom control
     var customPerson = new Person({
        items : {
            path : "/person",
            template : new PersonItems({ first : "{firstName}", last : "{lastName}"})
            } 
        });

    //Creating a button
    var button = new sap.ui.commons.Button('btn',{
        text : "Click",
        press : function(){
           //Update model property
            var model = sap.ui.getCore().getModel();
            model.setProperty("/person/0/lastName",'Dog');
        }
    })

    customPerson.placeAt('master');
    button.placeAt('master');

Despite the Press function of the button successfully modifying the model, the view remains unchanged. How can the view be updated upon clicking the button and updating the model?

Furthermore, when using the renderer function, an automatic creation of PersonRenderer.js occurs. Direct placement of the render function does not yield the desired result. Could this be due to aggregation binding? While SAPUI5 examples display working functionality, my implementation seems to encounter issues.

Answer №1

The object declaration begins by utilizing "lastName" as the attribute for the last name object. However, the PersonItems template binding references the control's property 'last' to the model property {last} - there is no property named 'last' in the model, only 'lastName'. Adjust the binding to utilize the property 'lastName' instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to trigger a directive using a function and then access the generated style?

My directive is designed to randomly select a color and assign it to a new user as an avatar. The random color generation and directive functionality are working as expected, but I'm looking to expand the functionality further and need some assistance ...

Visualize data from ajax call in tabular format

I want to display the results of an SQL query in a table using AJAX. I have written code that executes the query during the AJAX call, but when I try to display these values in a table, it shows null values on the div tag. What could be the reason for this ...

"Utilize jQuery to load a file when hovering over a specific

How can I dynamically load an external file using jQuery when a user hovers over a specific div? I attempted to load the file like a CSS file on hover but was unsuccessful. Is it possible to load a CSS file on hover as I've seen in some examples? $(d ...

Maintaining the original layout upon refreshing the page

Incorporating two forms on my website, I added a button that transitions between the login and sign-up forms smoothly. The process is as follows: Initially, the login form is displayed; clicking the button switches to the sign-up form. Proceeding to subm ...

The flickering problem with HTML5 DOM

I created a HTML5 game using canvas and DOM elements, but I'm facing an issue with flickering DOM elements while playing. The problem seems to be more prevalent in mobile browsers, particularly Chrome. My game includes a full screen canvas with vario ...

What is the best way to implement the Active list element feature in my menu bar?

The current list element is : <li class="nav__item"><a class="nav__link nav__link--active " href="#"... The standard list element is: <li class="nav__item"><a class="nav__link " href=&quo ...

Illustrate an element positioned beneath a child element within a different element

I am in the process of designing an overlay on a Google Map that resembles a real map placed on a table, using 2 pixel lines as creases above the map. This design does not significantly impact interactions with the map; only 6 out of 500 vertical lines are ...

What is the best way to extract properties from JSON data using Javascript?

The json output provided is as follows: { "categories":[ "Physical support- 18-64 (RA)" ], "series":[ { "name":"Buckinghamshire", "data":[ 9088 ] }, { "name":"Lincolnshire", ...

Encountering a blank response object when making a POST request with the fetch API

I'm new to working with express/Node.js. I recently attempted to send a request including the current user's location (longitude and latitude using the geolocation API). /public/index.html <!DOCTYPE > <html> <head> <me ...

Having trouble with a tslint error in Typescript when creating a reducer

I encountered an error while working with a simple reducer in ngRx, specifically with the on() method. https://i.sstatic.net/9fsvj.png In addition, I came across some errors in the reducer_creator.d.ts file: https://i.sstatic.net/sWvMu.png https://i.ss ...

How can I transfer an array of objects obtained from an ajax call to an observable array?

One of my functions involves making an ajax call and receiving data in the callback. It looks something like this: function fetchData(callback) { //perform ajax if(callback) { callback(data.data); } } If I use the function like this: fetc ...

Generating Unique Random DIV IDs with JavaScript

Does anyone know of a JavaScript solution to generate unique random DIV IDs? I am currently using PHP for this purpose. <?php function getRandomWord($len = 10) { $word = array_merge(range('a', 'z'), range('A', &apos ...

Top method for identifying "abandoned words" within text that has been wrapped

Our content and design teams have a specific request to prevent paragraphs from ending with an "orphan word" - a single word on the last line of text that has wrapped onto multiple lines. The designer's proposed solution is to adjust the margins sligh ...

Adding routes dynamically in nodeJS using Express is a powerful way to manage your server

Is there a way to dynamically add paths to nodeJS within a running program? Instead of relying on kludged solutions like resetting the server when files change, I am looking for a more efficient method. Any suggestions? EDIT: This project is focused on pr ...

Transferring values with jQuery

I attempted to customize the appearance of the select dropdown options, but unfortunately, the value of the options is not being transferred to the new jQuery-created class. Due to this issue, I am unable to achieve the desired outcome. The expected behavi ...

inputting a pair of parameters into a function

While creating an action for react-redux, I encountered a problem when trying to pass two variables into the function - dispatch and hosts. Strangely, the variable host is being logged as a function instead of its actual value. This is my code: export fu ...

Interactive Icon Feature Instead of Annoying Pop-Ups in JavaScript

Hello there! I need assistance fixing a code issue. Currently, the code automatically pops up when the page is opened. Is there a way to make it clickable instead of popping up automatically? <script type="text/javascript" src="//www.klaviyo.com/media/ ...

Tips for maintaining DataTables filters after navigating Back/Forward or refreshing the page

We are currently utilizing DataTables for our table, and we are encountering difficulties in retaining the history of filters that were previously applied to the table. This would allow users to navigate back and forth and refresh through these filters. O ...

Guide to implementing the onchange event in JavaScript for this specific scenario

How can the onchange event be utilized in this scenario using JavaScript? First, input data into the input with id="one". Subsequently, the data in the input with id="two" will be updated to match the data in the input with id="one". However, when the da ...

Struggling with implementing Vue.js for making a task list using Bootstrap 5

I'm trying to get the hang of Vue.js. I've been working on setting up a to-do list where users can input tasks, but I'm having trouble getting the list to display correctly when I define the method. It seems like my add() function isn't ...