dlds / yii2-jqhooks
Yii2 jQuery action hooks
Installs: 2 337
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:JavaScript
Type:yii2-extension
pkg:composer/dlds/yii2-jqhooks
Requires
- yiisoft/yii2: ~2.0.0
 
Requires (Dev)
This package is not auto-updated.
Last update: 2025-10-26 01:42:58 UTC
README
Action hooks is simple jQuery helper library. Used when jQuery operation like show, hide, toggle, ... are required to attach with conditional ability.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require dlds/yii2-jqhooks
or add
"dlds/yii2-jqhooks": "~1.0"
to the require section of your composer.json file.
Usage
You want to show 'overlay' html element when user clicks on 'trigger' html element. With Action hooks you have to do:
- Register hook
 
<div class="my-custom-overlay-class" data-hook="overlay"></div>
- Register trigger
 
<a class="my-custom-trigger-class" data-had="{"click":[["show","overlay"]]}"></div>
Each registered hook must have data-hook="{hookName}" attribute. Each trigger element must have data-had="{hookActionDefinition}" attribute
Format
Hook action definition (data-had) format:
{ 'jqEevent' => [ ['hookActionName' 'hookName', 'hookActionCondition'], ] }
Attributes
jqEventis classic jQuery event like 'change', 'click', ...hookActionNameis jqhooks aciton method name (doClose, doOpen, doToggle, ...).hookNameis custom string which reflects value of 'data-hook' attr assigned to target elementhookActionConditioncallback or simple fn definition which will be processed as js function. This condition allows you to define when the action will be processed.
Actions
doOpenadd class 'open' to hooked elementdoCloseremove class 'open' to hooked elementdoShowshows hooked element (calls $.show())doHidehides hooked element (calls $.hide())doToggletoggles hooked element (calls $.toggle())doCheckadds property 'checked=true' to hooked elementdoUncheckrmeoves property 'checked=true' to hooked element
Ternary usage
You can specify ternary condition like following:
{ 'click' => [ ['show:hide' 'overlay', 'return this.val() === 1'], ] }
This definitios runs 'show' action if clicked element value === 1 otherwise it runs 'hide' action.
Helper
You can simple use JqHooks::attach() which registeres proper js and runs initialization on document ready.
JqHooks::attach([ 'click' => [ ['show:hide' 'overlay', 'return this.val() === 1'], ] ])