Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-43281

Functions within JS object are lost when used in models

    XMLWordPrintable

Details

    Description

      As deponstrated in this testcase, javascript function are not preserved when used in object within a model.

      import QtQuick 2.3
      import QtQuick.Window 2.2
      
      Window {
      
          //Imagine this is a component that is meant to be re-usable
      
          // the users of this compontent would then fill the model like this
          property var actionList: [
              { text: qsTr("Hello"),
                enabled: true,
                action: function() {  console.log("Hello")  }
              },
              { text: qsTr("World"),
                enabled: true,
                action: function() {  console.log("World")  }
              }
          ];
      
          title: qsTr("Hello World")
          width: 640
          height: 480
      
          Column {
              width: parent.width;
              spacing: 10;
      
              Repeater {
                  id: rep
                  model: actionList;
                  Rectangle {
                      width: parent.width;
                      height: 60;
                      border.color: "black"
                      border.width: 2;
                      Text {
                          anchors.centerIn: parent;
                          text: modelData.text;
                      }
                      MouseArea {
                          anchors.fill: parent;
                          onClicked: {
                              if (modelData.action) {
                                  modelData.action(); // BUG!!! does not work
                                  //  TypeError: Property 'action' of object [object Object] is not a function
      
                                  // rep.model[index].action(); // Also does NOT work
                                  
                                  //Workaround:  this works
                                  //actionList[index].action();
                                  
                              }
                          }
                      }
                  }
      
              }
      
          }
      }
      
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              Unassigned Unassigned
              ogoffart Olivier Goffart (Woboq GmbH)
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes