简体   繁体   中英

What's the best practice for dealing with application states and event broadcasting in AngularJS?

I'm still struggling through my first Angular application and as one question is answered, more questions pop up.

I have an app which, when in "save mode," has a form. When the form is successfully completed and saved, I want to go show different content, let's call it "browsing mode." My current query is about how to handle this type of transition.

What entity should broadcast events? Should it be in the controller or in the resource which is doing the saving? Or somewhere else?

What entity should listen for and handle events? In the interest of loose coupling, the form controller should only broadcast "save completed" - not "browsing mode" because it doesn't/shouldn't know about the browse controller. So, what entity should receive "save completed" and know to broadcast "browse mode enabled"?

Here is my take on it

  1. Controller should broadcast event because it has the context and view changes are based based on this context. Resource can broadcast the event, but firstly it has to do on $rootScope and seconds it is a more generic broadcast, and hence if the resource saving happens from multiple places, differentiating it becomes again the job of controller.

  2. There should be a parent controller which should coordinate this process so, neither the 'save mode' controller or 'browsing mode' controller and any information about the view transitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM