This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const myEmitter = require('./my_emitter'); | |
| // Perform the registration steps | |
| // Pass the new user object as the message passed through by this event. | |
| myEmitter.emit('user-registered', user); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const myEmitter = require('./my_emitter'); | |
| myEmitter.on('user-registered', (user) => { | |
| // Send an email or whatever. | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const myEmitter = require('./my_emitter'); | |
| const sendEmailOnRegistration = require('./send_email_on_registration'); | |
| const someOtherListener = require('./some_other_listener'); | |
| myEmitter.on('user-registered', sendEmailOnRegistration); | |
| myEmitter.on('user-registered', someOtherListener); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = (user) => { | |
| // Send a welcome email or whatever. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const myEmitter = require('./my_emitter'); | |
| // Perform the registration steps | |
| // The application should react differently if the new user has been activated instantly. | |
| if (user.activated) { | |
| myEmitter.emit('user-registered:activated', user); | |
| } else { | |
| myEmitter.emit('user-registered', user); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const myEmitter = require('./my_emitter'); | |
| const sendEmailOnRegistration = require('./send_email_on_registration'); | |
| const someOtherListener = require('./some_other_listener'); | |
| const doSomethingEntirelyDifferent = require('./do_something_entirely_different'); | |
| myEmitter.on('user-registered', sendEmailOnRegistration); | |
| myEmitter.on('user-registered', someOtherListener); | |
| myEmitter.on('user-registered:activated', doSomethingEntirelyDifferent); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChatUser { | |
| displayNewMessageNotification(newMessage) { | |
| // Push an alert message or something. | |
| } | |
| // `chatroom` is an instance of EventEmitter. | |
| connectToChatroom(chatroom) { | |
| chatroom.on('message-received', this.displayNewMessageNotification); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = (user) => { | |
| setImmediate(() => { | |
| // Send a welcome email or whatever. | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PostsController < ApplicationController | |
| def index | |
| posts = Post.all | |
| render json: posts | |
| end | |
| end | |
| class Post | |
| belongs_to :author, class_name: 'User' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PostsController < ApplicationController | |
| def index | |
| # Runs a SQL join with the users table. | |
| posts = Post.includes(:author).all | |
| render json: posts | |
| end | |
| end |