[wp-trac] [WordPress Trac] #21170: JavaScript actions and filters

WordPress Trac noreply at wordpress.org
Thu Oct 1 00:03:22 UTC 2015


#21170: JavaScript actions and filters
------------------------------------+------------------------------
 Reporter:  koopersmith             |       Owner:  adamsilverstein
     Type:  feature request         |      Status:  assigned
 Priority:  normal                  |   Milestone:  Future Release
Component:  General                 |     Version:  3.4
 Severity:  normal                  |  Resolution:
 Keywords:  has-patch dev-feedback  |     Focuses:  javascript
------------------------------------+------------------------------
Changes (by adamsilverstein):

 * keywords:  2nd-opinion has-patch dev-feedback => has-patch dev-feedback


Comment:

 In [attachment:21170.diff]:

 Add the events library created by @carlcanley in this
 [https://github.com/carldanley/WP-JS-Hooks GitHub Repo] (renamed as wp-
 hooks for consistency.

 Merging this code will make no change in core actions yet. We can add them
 going forward and use a helper wrapper to update existing events for
 backwards compatibility (so existing document.trigger's still get fired).
 I wanted to keep the patch as small as possible. It would make sense to
 add [https://github.com/carldanley/WP-JS-Hooks/tree/master/test repo unit
 tests] to core as well when merging, I can work on adding that to the
 patch.

 With this patch, plugins, themes and other core patches could start using
 wp-hooks by simply adding 'wp-hooks' as a script dependency in
 `wp_enqueue_script` or enqueueing directly with `      wp_enqueue_script(
 'wp-hooks' );`

 Notes from the GitHub repo:

 ### Dependencies
 WP-JS-Hooks does not require any third-party applications or software. The
 library is completely self-maintained. However, the repository itself uses
 Grunt to perform tasks such as JSHint, Uglify, and QUnit.

 ### API Usage
 API functions can be called via the global `wp.hooks` like this
 `wp.hooks.addAction()`, etc.

 * `addAction( 'namespace.identifier', callback, priority )`
 * `addFilter( 'namespace.identifier', callback, priority )`
 * `removeAction( 'namespace.identifier' )`
 * `removeFilter( 'namespace.identifier' )`
 * `doAction( 'namespace.identifier', arg1, arg2, moreArgs, finalArg )`
 * `applyFilters( 'namespace.identifier', content )`

 ### Features
 * Fast and lightweight, only 1.3kb
 * Priorities system ensures hooks with lower integer priority are fired
 first.
 * Uses native object hash lookup for finding hook callbacks.
 * Utilizes insertion sort for keeping priorities correct. Best Case: O(n),
 worst case: O(n^2)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/21170#comment:100>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list