Little change in the event system (raiseEvent)

Hi,

This isn’t really a request, just a little suggestion/idea regarding the event system, but it felt like the appropriate place to post.

I’m currently working on a little project of mine, which happens not to be Yii-related (cause it’s command-line and w/out database…) but where I wanted to have an event system, which turned out to be similar/inspired by Yii’s implementation. Of the little differences I made, here’s one that I thought might turn useful if implemented in Yii as well, possibly.

Right now, the onXXX methods are well as raiseEvent require a param $event, the event to be raised. Why not make it optional? That is, if it’s not specified then raiseEvent creates it automatically. This is simply to save a few characters to be typed, when we don’t need to do anything specific other than raising a CEvent. E.g:




// instead of:

$this->onFoobar(new CEvent($this));

// we'd do this:

$this->onFoobar();



Obviously this only applies when raising an event on the current object ($this) so that raiseEvent can set the sender, but it might (should?) be the case most of the times, no? (Besides, this would only be an addition, leaving the current syntax fully supported of course.)

Also, you could even have raiseEvent return the event when done (and of course, all the onXXX methods would need to be updated as well), so if one needs to raise an event, and then needs to use it, e.g. to check if it’s been handled or not, you could do:




$event = $this->onFoobar();

// or, to only check if it's been handled or not:

if (!$this->onFoobar()->handled)



Additionally, you could even accept an array as parameter $event, to automatically set some properties. This would allow something like this:




if (!$this->onFoobar(array('foo' => 'bar', 'answer' => 42)->handled)

// or maybe even:

if (!$object->onFoobar(array('sender' => $this, 'foo' => 'bar', 'answer' => 42)->handled)



It seems from the code that right now one can use anything as $event (CEvent or a child class, but also anything else, including int, string, etc) while the documentation states it must be a CEvent, so I’m not sure if doing so would break anything or not. Shouldn’t if the rules from the documentation have been followed though, right?

Anyways, just an idea :slight_smile:

-pinpin

I am not able to figure out what this instance of CEvent is doing in the function "raiseEvent".I realy wasted lot of time.

Suppose we have 2 global functions;




function greetingOne($name){

echo "Hello ".$name;

}

function greetingTwo($name){

echo "Hello ".$name;

}



Now let us have an event as "onMeeting";

Let us attach the previously declared functions to our event;




$component->onMeeting='greetingOne';

$component->onMeeting='greetingTwo';



Now I am having the trouble. How can I raise the event with different parameter for each function.

I want to pass "Jack" to the greetOne and "Jill" to greetTwo.

The following are not working.




$event=new CEvent($this,'Jack','Jill');

$component->raiseEvent('onClick',$event);

$component->raiseEvent('onClick','Jack','Jill');

$component->raiseEvent('onClick',array('Jack','Jill'));



As event is important concept in Yii.I am desperate in knowing about it.

Can any one please help?




function greetingOne(CEvent $event)

{

	echo 'Hello ' . $event->params['person1Name'];

}


function greetingTwo(CEvent $event)

{

	echo 'Hello ' . $event->params['person2Name'];

}


$event = new CEvent($this, array('person1Name' => 'Jack', 'person2Name' => 'Jill'));

$component->raiseEvent('onMeeting', $event);



BTW, the portion of your code that triggers an event must not have any assumptions about the number, order or role of handlers assigned to the event. When you have to send different data for each event handler, it indicates a problem with your design and/or a bit of misuse of events.