Support

Account

Home Forums General Issues Extending field in JavaScript, can I call the parent method?

Solving

Extending field in JavaScript, can I call the parent method?

  • Hello, If I extend an existing field in javascript with

    const ImageExtends = acf.models.ImageField.extend({

    And I wish to create a replacement function, lets say removeAttachment, is there a way to invoke the parent method so I can avoid having to duplicate the original method/functions body? I still want to call the original remove, but do some extra work either side of that call.

    eg the PHP equivalent in a class is parent::someMethod() from within someMethod on child.

    I found that…

    // Set a convenience property in case the parent's prototype is needed later.
    //Child.prototype.__parent__ = Parent.prototype;

    …is commented out, does this elude to this being intentionally disabled?

  • When extending fields in JavaScript using ACF’s model system, it’s absolutely possible to call the parent method from within your extended method, similar to how you would in PHP with parent::method().

    You can do this by using this._super() in your child method to invoke the parent method. For example, in your case, to extend the removeAttachment function, you could structure it like this:

    javascript
    Copy code
    const ImageExtends = acf.models.ImageField.extend({
    removeAttachment: function() {
    // Call the parent method
    this._super();

    // Your extra logic here
    console.log(“Doing extra work after removing attachment…”);
    }
    });
    The this._super() call refers to the parent method, allowing you to avoid duplicating its logic. This should help you keep your calendar code clean and modular.

    Hope this helps !

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.