g3w-suite / g3w-client-plugin-editing

https://g3w-suite.readthedocs.io/en/latest/g3wsuite_editing.html
0 stars 3 forks source link

Invalid context for `this._saveFeatures` within `OpenFormTask::_saveFnc` ? #84

Closed Raruto closed 10 months ago

Raruto commented 10 months ago

The following function appears to be unused:

https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L234

because it is probably called with the wrong context in here (ref: return function():

https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L297-L300

https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L359

volterra79 commented 10 months ago

The following function appears to be unused:

https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L234

because it is probably called with the wrong context in here (ref: return function():

https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L297-L300

https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L359

@Raruto it used https://github.com/g3w-suite/g3w-client-plugin-editing/blob/edd33b1dd02cdeaff676799942928f3ad899e96e/workflows/steps/tasks/openformtask.js#L297-L300 as you report.

Raruto commented 10 months ago

it used

@volterra79 vscode editor shows me as unusued..

The "this" context is wrong.

That means, this._saveFnc(promise, context, inputs).bind(this) doesn't work as you would excpect, because:

proto._saveFnc = function(promise, context, inputs) { 
  return function(fields) {     // <-- this is NOT an arrow function (ie. () => { this._saveFeatures({ .. }); }),
    this._saveFeatures({ .. }); // so the "this._saveFeatures" no longer refers to class method
  }
}
volterra79 commented 10 months ago

it used

@volterra79 vscode editor shows me as unusued..

The "this" context is wrong.

That means, this._saveFnc(promise, context, inputs).bind(this) doesn't work as you would excpect, because:

proto._saveFnc = function(promise, context, inputs) { 
  return function(fields) {     // <-- this is NOT an arrow function (ie. () => { this._saveFeatures({ .. }); }),
    this._saveFeatures({ .. }); // so the "this._saveFeatures" no longer refers to class method
  }
}

@Raruto

cbk: this._saveFnc(promise, context, inputs).bind(this) returns a function that is bind to OpenFormTask

It used by form save button to has a refence to openformtask instance.

Did you try to log this?

proto._saveFnc = function(promise, context, inputs) {
  return function(fields) {
    //console.log(this)
    const session = context.session;
    this._saveFeatures({
      fields,
      promise,
      session,
      inputs
    });
  }

};

Raruto commented 10 months ago

Damn parentheses..

this._saveFnc(promise, context, inputs).bind(this)

In your opinion, is there a simple way to rewrite it like the following?

this._saveFnc(this, promise, context, inputs)
volterra79 commented 10 months ago

Damn parentheses..

this._saveFnc(promise, context, inputs).bind(this)

In your opinion, is there a simple way to rewrite it like the following?

this._saveFnc(this, promise, context, inputs)

@Raruto or just change

this._saveFnc(promise, context, inputs)
proto._saveFnc = function(promise, context, inputs) {
  return (fields) =>  {
    const session = context.session;
    this._saveFeatures({
      fields,
      promise,
      session,
      inputs
    });
  }
volterra79 commented 10 months ago

@Raruto check commit https://github.com/g3w-suite/g3w-client-plugin-editing/pull/83/commits/d296ecb7599720dd80038b50465c8a3986f819a4

volterra79 commented 10 months ago

@Raruto we can close it?