I. How do you understand the publish subscription model?
- JS in the design mode:
- Singleton mode: Handling business logic
- Construct prototype mode: Encapsulate Class Library, component, frame, plugin, etc.
- Class Library: JQuery
- Just provides a few common methods that can be applied to any project, not business
- Component: Bootstrap
- A lot of common components are provided (Html/css/js are prescribed by others), we only need to use as required, we can directly achieve the effect
- Plugin: Swiper/iscroll
- Package for a specific business, such as a tab plug-in or a Carousel diagram plugin
- Frame: React/vue
- A certain programming idea (MVC/MVVM) is called a frame
- Publish subscription Model: to handle some specific requirements
- Promise mode: Dealing with some specific needs
- Publish subscription Mode
- Publish a schedule (publish)
- Add something to your schedule (subscribe)
Two. Publish subscription mode
Publish subscription mode is not a dead mechanism, he is a kind of thought, a form of writing code, mainly in order to deal with a one-to-many scenarios, applied to different functions of the call, this is very important
Manually simulate the browser event mechanism
- Subscription
- Release
Change this point
- Create a small tool box
- Put our methods in the Little tool box.
- When we want to use this method, we can go directly to the Small tool box variable.
The problem of dealing with order, in fact, is to deal with the problem of IE event pool chaos, then we can not use the event pool of IE, we simulate an event pool (using the idea of publishing subscriptions to simulate our event pool)
- Add handlers when listening
- The handlers for the events that are being monitored are executed at the time of release
The Publish-subscribe mode in JS Event