Hello
How to create event for component in its behavior class?
Hello
How to create event for component in its behavior class?
Hey abbish,
Could you please explain a bit more about it.SO, that anybody can suggest something.
Ok,
//I have a behavior to extend component features
class sampleBehavior extend CBehavior
{
public function doSomthing()
{
//i hope can create some event at here for this behavior
if($this->hasEventHandler('onBeforeDoSomthing')) $this->onBeforeDoSomthing(new CEvent());
....
if($this->hasEventHandler('onAfterDoSomthing')) $this->onAfterDoSomthing(new CEvent());
}
}
//then, i have another behavior will inherit sampleBehavior class
class sampleChildBehavior extend sampleBehavior
{
//i hope customize doSomthing method's feature with onBeforeDoSomthing & onAfterDoSomthing event like CComponent's event
}
Could this work ?
i found that the CBehavior class also be a component instance, but the event method which created in behavior is not available when it attach to a component
I am not much clear about what you intend.
Check whether the following is beneficial.
class testBehavior extends CBehavior
{
public function greet()
{
echo "hello ";
}
public function events()
{
return array
(
'onMeeting'=>'greet',
);
}
}
class testComponent extends CComponent
{
public function onMeeting($event)
{
$this->raiseEvent('onMeeting',$event);
}
}
$obj=new testComponent;
$behavior=new testBehavior;
$event=new CEvent;
$obj->attachBehavior('name',$behavior);
//Now rise the event
$obj->onMeeting($event);
//The output would be "hello".