我正在使用oxwall
,我想了解EventManager
如何在此平台上运行,以便在特定事件上注册新方法,并使用一个简单示例来触发事件并将事件绑定到流程。
答案 0 :(得分:1)
以下是触发事件并将事件绑定到进程的示例。我的格式有点糟糕......
假设您正在创建用于创建用户帐户的插件。
<div flex layout="row" layout-align="center center">
<div flex-sm="100" flex-gt-sm="90" flex-gt-md="70" flex-gt-lg="50" class="md-whiteframe-z2">
<md-content class="md-padding">
<div flex-sm="100" flex-gt-sm="80" layout-sm="column">
<form name="contactForm" data-ng-submit="sendMail()">
<md-input-container>
<label>Name:</label>
<input ng-model="contactName" required>
</md-input-container>
<md-input-container flex>
<label>Email:</label>
<input type="email" ng-model="contactEmail" required>
</md-input-container>
<md-input-container>
<label>Message:</label>
<textarea ng-model="contactMsg" columns="1" md-maxlength="150" required></textarea>
</md-input-container>
̶<̶b̶u̶t̶t̶o̶n̶ ̶t̶y̶p̶e̶=̶"̶s̶u̶b̶m̶i̶t̶"̶>̶S̶e̶n̶d̶<̶/̶b̶u̶t̶t̶o̶n̶>̶
<md-button type="submit" class="md-primary"
ng-class="{'md-raised md-hue-1': (contactForm.$dirty && contactForm.$valid) }"
aria-label="Send">Send</md-button>
</form>
</div>
</md-content>
</div>
</div>
将事件绑定到听众
您应该从事件处理程序类或插件init.php文件中执行此部分。
/**
* Method to save user
*/
public function saveUser( $username, $password, $email, $accountType )
{
$userService = BOL_UserService::getInstance();
$user = $userService->createUser( $username, $password, $email, $accountType );
//Set new event parameters
$event = new OW_Event('plugin_key_custom_event_name', array(
'userDto' => $user
));
//Trigger an event for after registering user
OW::getEventManager()->trigger($event );
}
最后,您可以从init.php文件初始化事件处理程序。
/**
*
* Bind Event
*/
class PLUGINKEY_CLASS_EventHandler
{
public function sendUserWelcome( OW_Event $e )
{
$params = $e->getParams();
$user = $params['userDto'];
BOL_UserService::getInstance()->sendWellcomeLetter($user);
}
public function init()
{
//bind sendUserWelcome() method to 'plugin_key_custom_event_name' event
OW::getEventManager()->bind('plugin_key_custom_event_name', array($this, 'sendUserWelcome'));
}
}