$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - What do the args for Gutenberg subpackage "hooks" function "doAction" mean?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - What do the args for Gutenberg subpackage "hooks" function "doAction" mean?

matteradmin10PV0评论

The package is supposed to mime WP's own actions system:

I've installed it locally and loaded up all of its modules, unfortunately, I seriously don't understand what the functionName and callback are in the case of addAction.

I tried messing with it quite a bit but no success. I thought functionName is the function I'm hooking to an action, for example:

var actions_api = createHooks();
function testFunction( a ) {
    console.log(a);
}
actions_api.addAction( 'testHook', 'global', 'testFunction', function( a ) {
    console.log(a);
}, 10 );
actions_api.doAction( 'testHook', 'super_arg' );

Returns the error The hook callback must be a function., while the code above seems stupid, I tried to mess with it a bit but no success.

What am I missing? In theory, there shouldn't be a need for a callback for an action.

The package is supposed to mime WP's own actions system:

https://github/WordPress/gutenberg/tree/master/packages/hooks

I've installed it locally and loaded up all of its modules, unfortunately, I seriously don't understand what the functionName and callback are in the case of addAction.

I tried messing with it quite a bit but no success. I thought functionName is the function I'm hooking to an action, for example:

var actions_api = createHooks();
function testFunction( a ) {
    console.log(a);
}
actions_api.addAction( 'testHook', 'global', 'testFunction', function( a ) {
    console.log(a);
}, 10 );
actions_api.doAction( 'testHook', 'super_arg' );

Returns the error The hook callback must be a function., while the code above seems stupid, I tried to mess with it a bit but no success.

What am I missing? In theory, there shouldn't be a need for a callback for an action.

Share Improve this question edited Jan 15, 2019 at 2:31 coolpasta asked Jan 15, 2019 at 2:25 coolpastacoolpasta 9691 gold badge9 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

I suggest to take a look at the tests file to see different ways of using hooks.

They work similarly to PHP hooks, you can set several addAction and then one doAction with the name of the hook and other arguments.

The first argument of doAction has to be the action name. Then you can pass as many arguments as you want and those will pass to the addAction callback function:

const myFunction = ( arg1, arg2 ) => {
    console.log( arg1, arg2 ); // Should output 'Hello' 'Hola'
};

wp.hooks.addAction( 'action_name', 'function_name', myFunction );

wp.hooks.doAction( 'action_name', 'Hello', 'Hola' );

The first argument is the hook name. Several addAction (or addFilter) can hook into the given hook by its name and all will be executed when calling doAction (or applyFilters). The second argument identifies the action or filter (addAction or addFilter). This is used, for example, if you want to remove one of the actions or filters. This is an adapted example taken from one of the tests:

const { addFilter, removeFilter, applyFilters } = wp.hooks;

function filterA(str) {
    return str + "A";
}

function filterB(str) {
    return str + "B";
}

function filterC(str) {
    return str + "C";
}

addFilter("test.filter", "my_callback_filter_a", filterA);
addFilter("test.filter", "my_callback_filter_b", filterB);
addFilter("test.filter", "my_callback_filter_c", filterC);

removeFilter("test.filter", "my_callback_filter_b");

console.log(applyFilters("test.filter", "test_")); // Should output test_AC
Post a comment

comment list (0)

  1. No comments so far