Yii Framework Forum: Little change in the event system (raiseEvent) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Little change in the event system (raiseEvent) Make $event optional Rate Topic: -----

#1 User is offline   pinpin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 23-February 11

Posted 23 February 2011 - 08:17 AM

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 :)
-pinpin
1

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 28 July 2012 - 03:28 PM

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?
0

#3 User is offline   phtamas 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 29 July 2012 - 12:07 PM

View Postseenivasan, on 28 July 2012 - 03:28 PM, said:

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.


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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users