qooxdoo / qooxdoo

qooxdoo - Universal JavaScript Framework
http://qooxdoo.org
Other
764 stars 259 forks source link

Deferred init not working #10670

Closed johnspackman closed 4 days ago

johnspackman commented 2 months ago

It looks like the deferred init / aka init defined by constructor does not work; the docs say that you can use the initXxx() method to change the initialisation value, and this is a key reason for having the initXxx function - but I'm finding that it ignores the actual value passed into initXxxx().

This seems like a substantial and surprising regression that crept in at some point, please can I have some extra eyes to sanity check my example?

Here's the documentation: https://archive.qooxdoo.org/current/pages/core/defining_properties.html

Here's the example code, also available at https://tinyurl.com/2mbuuum2

qx.Class.define("pkg.MyClass", {
  extend: qx.core.Object,

  construct: function() {
    this.base(arguments);
    console.log("pkg.MyClass constructor");

    this.addListener("changeAlpha", evt => console.log("changeAlpha " + evt.getData()));
    this.addListener("changeBravo", evt => console.log("changeBravo " + evt.getData()));
    this.addListener("changeCharlie", evt => console.log("changeCharlie " + evt.getData()));

    this.initAlpha(3);
    this.initAlpha(4);
    this.initBravo(6);
    this.initCharlie(9);
  },

  properties: {
    alpha: {
      init: 2,
      apply: "_applyAlpha",
      event: "changeAlpha"
    },
    bravo: {
      init: 5,
      apply: "_applyBravo",
      deferredInit: true,
      event: "changeBravo"
    },
    charlie: {
      apply: "_applyCharlie",
      deferredInit: true,
      event: "changeCharlie"
    }
  },

  members: {
    _applyAlpha(value) {
      console.log("applyAlpha value=" + value);
    },
    _applyBravo(value) {
      console.log("applyBravo value=" + value);
    },
    _applyCharlie(value) {
      console.log("applyCharlie value=" + value);
    }
  }
});

var x = new pkg.MyClass();
console.log("alpha=" + x.getAlpha());
console.log("bravo=" + x.getBravo());
console.log("charlie=" + x.getCharlie());

The output is:

pkg.MyClass constructor
applyAlpha value=2
changeAlpha 2
applyBravo value=5
changeBravo 5
applyCharlie value=9
changeCharlie 9
alpha=2
bravo=5
charlie=9
derrell commented 2 months ago

Something, somewhere, says that if you use deferredInit then you do not use init, and I'm pretty sure that's what I found was the case when reimplementing properties. Once a property value has been initialized, e.g., with init, it can't be changed with another initialization, in this case, initX(). Exclude the init members of the property descriptors, and it should work as expected.

johnspackman commented 2 months ago

Ah - thank you that makes sense, although it seems a bit odd; it seems really odd that the initXxx function will call apply and fire events, whether defining the initial value or not.

goldim commented 4 days ago

@johnspackman Is your problem solved? May I close it?

johnspackman commented 4 days ago

@goldim yes :)