Giter VIP home page Giter VIP logo

Comments (7)

samdark avatar samdark commented on July 24, 2024

Not quite sure what are you talking about exactly. Yii2 has event support i.e. you can do

$component->trigger('myEvent');

and then anything can subscribe to an event like the following:

$component->on('myEvent', function($event){ ... });

What's different from 1.1 is syntax and the fact that you don't need to declare event prior to raising it.

from yii2.

samdark avatar samdark commented on July 24, 2024

Moreover, with typical application flow there's only one module running at a time and we aren't going to change it.

from yii2.

bwoester avatar bwoester commented on July 24, 2024

👍 for inter module communication.

Would be really cool if we could do this from module 1:

Yii:app()->trigger( 'mod_1.xyz', new Mod1Event() );

and this in config or onAppInit():

Yii::app()->on('mod_1.xyz', function(Mod1Event $event) {
  Yii::app()->getModule('mod_2')->abc( $event->whatever );
});

And I think it should be possible with the new event system, shouldn't it?

from yii2.

qiangxue avatar qiangxue commented on July 24, 2024

Yes, this is possible in Yii 2. You can define and use whatever events.

from yii2.

mashhadi avatar mashhadi commented on July 24, 2024

cool

from yii2.

yiqing-95 avatar yiqing-95 commented on July 24, 2024

@mashhadi i think i fully understand what 's your meaning !
in sns project there are some scenario need modules comunication ! say that in user module if a user be deleted and this action will affect some other modules like : blog ,photo , event .. . (if a user will be deleted ,you should also delete all of his/her resources)

even yii2 support new "event" model, the above scenario should be designed carefully . lets say yii2 's new event system:
Yii:app()->trigger( 'mod_1.whatEverStringNameAsEvent', new CEvent() );

Yii::app()->on('mod_1.whatEverStringNameAsEvent, function(CEvent $event) {
Yii::app()->getModule('mod_2')->abc( $event->whatever );
});
it can treat any string as a event id , not same as the event system of yii1.x (you must predefine a method as a event)

here i just give my way : you first define what event may be triggerred from one module , then use db to hold the listener list from another modules . when a module event fired the first thing is to look for all the listeners which listen to this event!
and then invoke these listeners one by one(surely you can define priority level to same eventId in the table).

for now yii2 (even sf1 eventDispatcher ) event system handle this thing is some thing like this : you first should register the listeners , you see you must "listen" the specific event before the event triggerred in same request thread ! but this may be wasteful if that event do not fire ( a event may be only triggerred on some specific condition ) . so even the yii2 support unpredefined event model , you will have to design it yourself to handle the module comunication !

in sns project the user related modules will not be foreknew (both the number and the content type !). usually these module can be installed and unistalled automatically , you can register the module event listeners in the install process ( you need insert records to module_event and module_listener table :) ,and do it in a reverse way in the uninstall process)!

from yii2.

mashhadi avatar mashhadi commented on July 24, 2024

@yiqing-95 yes, i exactly mean the same thing, i have already implemented a similar approach but not involving any db, In every module i have a Event Handler class, i write all handlers in this class. Whenever an event is raised it check for EventHandler classes in all modules and call respective handlers. Your approach seems to be better as you can prioritize your handlers, but in my case that is not needed.
I really appreciate your thoughts, it will help me in future.

from yii2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.