Skip to content

JavaScript Handling

Chase Wiseman edited this page Apr 4, 2020 · 10 revisions

A new SkyVerge\WooCommerce\PluginFramework\vX_Y_Z\Frontend\Script_Handler abstract class was added in version 5.6.2. The existing payment gateways frontend handler classes (SV_WC_Payment_Gateway_Payment_Form, SV_WC_Payment_Gateway_My_Payment_Methods and SV_WC_Payment_Gateway_Apple_Pay_Frontend) now extend this class.

PHP Handler Changes

Any custom handler enqueuing a JavaScript class initialization should:

  • Extend (directly or indirectly) the Script_Handler class
  • Define a $js_handler_base_class_name protected attribute, containing the JavaScript handler base class name, without the framework version namespace, e.g., $this->js_handler_base_class_name = 'SV_WC_Payment_Form_Handler';
  • Deprecate its own get_js_handler_name() method, if it has one, and replace calls to it by calls to the parent method
  • In the enqueued JavaScript:
    • Define a load function, instantiating the JavaScript class returned by get_js_handler_class_name() with the args returned by get_js_handler_params()
    • In a try/catch block, check if the class is available and call the load function
    • If the class is not available and in the corresponding catch block:
      • Add a listener to the corresponding JavaScript event, returned by get_js_loaded_event()
      • Echo $this->get_js_handler_event_debug_log_request(); to echo the script that makes the AJAX call to log errors initializing JavaScript handlers

See SV_WC_Payment_Gateway_Payment_Form::render_js() for an example.

JavaScript Handler Changes

Any custom JavaScript handler class should:

  • Be namespaced with the framework version, e.g., SV_WC_Payment_Form_Handler_v5_6_2
  • After the class definition, dispatch a JavaScript event, e.g., $( document.body ).trigger( "sv_wc_payment_form_handler_v5_6_2_loaded" )

See SV_WC_Payment_Form_Handler_vX_Y_Z for an example.