WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 months ago

Last modified 2 months ago

#19675 closed enhancement (fixed)

Add a jQuery triggers to the widget save action

Reported by: digitalnature Owned by: ocean90
Milestone: 3.9 Priority: normal
Severity: normal Version: 3.3
Component: Widgets Keywords: has-patch
Focuses: Cc:

Description

Line 241 (widgets.dev.js):

$(document).trigger('saved_widget);

or something...

This way we could nicely hook our own js on that "saved_widget" event, instead of stuffing our js inside the widget form function.

This is relevant for all events created by WordPress in the administration area, not just widget-save.

Think of it like "add_action" for javascript...

Attachments (1)

19675.patch (1.8 KB) - added by ocean90 3 months ago.

Download all attachments as: .zip

Change History (15)

comment:1 scribu3 years ago

  • Cc scribu added

comment:2 johnbillion3 years ago

  • Cc johnbillion@… added

comment:3 dgwyer11 months ago

  • Cc d.v.gwyer@… added

comment:4 ocean905 months ago

  • Milestone changed from Awaiting Review to Future Release
  • Severity changed from major to normal

Think of it like "add_action" for javascript...

Related: #21170

comment:5 westonruter4 months ago

Without a specific event which fires when a widget form gets replaced, it seems the de facto workaround is to hack jQuery.ajaxSuccess with checks for whether the the request for updating the widget. See an example at: http://wordpress.stackexchange.com/q/138355/8521

In #27491 I've just supplied a patch which adds a widget-form-update jQuery event which fires on the widget's root element whenever the form gets updated (i.e. when it is saved on the widgets admin page), and when this event happens the widgets can re-initialize any dynamically-generated fields (such as Chosen select fields). The event is also triggered in the customizer, where there are both hard updates and soft updates depending on whether it is detected that live updates are supported.

For an example of how such a widget may use this widget-form-update, see this sample plugin: https://gist.github.com/westonruter/9676069

comment:6 ocean904 months ago

  • Milestone changed from Future Release to 3.9
  • Owner set to ocean90
  • Status changed from new to accepted

Moving to 3.9 because of #27491.

ocean903 months ago

comment:7 ircbot3 months ago

This ticket was mentioned in IRC in #wordpress-dev by ocean90. View the logs.

comment:8 ocean903 months ago

In 27909:

Widget Customizer: Improve support for dynamically-created inputs.

  • Re-work how and when widget forms get updated.
  • Replace ad hoc hooks system with jQuery events,
  • Add widget-updated/widget-synced events for widget soft/hard updates.
  • Enter into a non-live form update mode, where the Apply button is restored when a sanitized form does not have the same fields as currently in the form, and so the fields cannot be easily updated to their sanitized values without doing a complete form replacement. Also restores live update mode if sanitized fields are aligned with the existing fields again.

Note: jQuery events are *not* final yet, see #19675.

props westonruter.
see #27491.

comment:9 westonruter3 months ago

  • Keywords has-patch added

comment:10 ircbot3 months ago

This ticket was mentioned in IRC in #wordpress-dev by ocean90. View the logs.

comment:11 renak-zillow3 months ago

It's nice to see the widget-updated/widget-synced/widget-added events in the customizer. Will those be implemented into the Widgets admin page as well?

comment:12 ocean903 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 27969:

Widgets: Trigger jQuery events for widget updates.

  • widget-added when a widget is added to a sidebar
  • widget-updated when a widget is updated

A jQuery object of the widget is passed along to the event handler. Same events are used in the Widget Customizer, see [27909].

fixes #19675.

comment:13 ocean903 months ago

I have ignored 19675.patch for now. In future we should additionally pass a real Backbone model to the event handler.

comment:14 ocean902 months ago

#19587 was marked as a duplicate.

Note: See TracTickets for help on using tickets.