Hi Harri, > Hi, > > Perhaps like most javascript coders, I'm mainly educated by experimenting > with it. > > I have a pretty strong Python background though, and most of the time I can > carry over some usage patterns just guessing how it probably works in js/qml > side. > > Now I was surprised though, and I wonder if the following behaviour is a bug, > or what is going on. > > I have some code, roughly like > > A.qml: > Item { > property variant cb // callback > > signal close() > > onClosed: cb() > > function popup(callback) { > cb = callback // callback() is called in this assignment!? > } > > ... > } > > > then some > B.qml: > > Item { > A { id: hello } > > function continueHere() { > print "should continue here" > } > > function setup() { > hello.popup(continueHere) > } > } > > > My problem is that callback is called at assignment time (see comment in > code). > Is it an artifact of cb being a variant property and I should not be doing > this? >
There are a couple of reasons why this doesn't work for your code: Firstly, the property is a variant property (which internally is a QVariant and so cannot store a JavaScript function value). Secondly, (and the real reason), we've currently defined the semantic that assigning a function to a property causes a binding assignment. A binding assignment causes the bound expression to be evaluated, and all of the properties involved in the expression to be "captured" (which means we automatically re-evaluated the expression if any of those properties change). In short, the behavior you are seeing is expected. However, for QtQuick2.0, we are looking at ways to allow a function value to be saved to a "var" property (or perhaps to a new property type). We are currently considering several options: 1) adding a new property type: "property function someProp" to which js function values can be assigned 2) adding a new function to the Qt global object: Qt.assignableFunction("expr") -- if the result is assigned to a var property, it is NOT considered a binding assignment 3) changing the semantic so that a function assignment is NOT considered to be a binding assignment, and instead adding a new function to the Qt global object: Qt.binding("expr") -- if the result is assigned to any property, it is evaluated as a binding assignment. Note that we do need some way to define binding assignments (eg for initial property values in dynamic object instantiation (loader, component), and for generating bindings in imperative js code), but as you point out, there are some good use-cases for being able to assign a js function value to a var property. The current workaround is to store a js array with a single element (the function value) to a var property. But obviously that's not a desirable situation. Cheers, Chris. > > Thanks, > > Harri > > > > _______________________________________________ > Qt-qml mailing list > Qt-qml@qt.nokia.com > http://lists.qt.nokia.com/mailman/listinfo/qt-qml _______________________________________________ Qt-qml mailing list Qt-qml@qt.nokia.com http://lists.qt.nokia.com/mailman/listinfo/qt-qml