How to accessing defaults variable in oneditprepare

I want to access a variable defined in my html file in defaults inside my oneditprepare function.

  RED.nodes.registerType("myNode", {
    category: "Custom",
    color: "#07950D",
    defaults: {
      name: { value: "" },
      timezone: { value: "" }, //accessing this in oneditprepare
    },
....
}

I am not sure how I can access it.

Place a debugger statement into your oneditprepare and then look around in the browser's debugger :wink:
It's something like this._def.defaults. Note that these are the defaults, not the current value. To access the "currently saved" value it's just this.<variable>. If you want the current unsaved value then you have to go chase it down in the DOM input element. If you find this confusing and unwieldy: welcome to the club!

oneditprepare inherits this, so this.timezone gives you access. Of course, the use of this must be treated carefully since going into a function - such as a jQuery callback function, changes what this is. So it is common to create a reference to the "correct" this early in the oneditprepare function. If you are calling your own functions, you can, of course, force this to be the correct one.

For myself, I never do inline code for oneditprepare, I always do this: oneditprepare: function() { onEditPrepare(this) },. Then my onEditPrepare function is defined as: function onEditPrepare(node) { ... }. I could have have used oneditprepare: function() { onEditPrepare.bind(this) }, to force my function to have the correct this but I find it easier to just pass the reference onto node as it reduces confusion in my tiny brain.

To TVE's point about DOM values. The easiest thing is to assume that this.timezone contains a value that was the last user input, not the current input. So $('#node-input-timezone').val() will (mostly - except for things like select drop-down's) give you the current input.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.