M
M
myspace2017-03-07 18:41:52
JavaScript
myspace, 2017-03-07 18:41:52

With this approach, always use bind?

Such a code troller, for example

class Index {

    constructor(title) {
        this.title = title;
    }

     mainPage(req, res, next) {

         let data = {
             title: this.title
         };

        res.render('index', data);

    }

}
module.exports = new Index('Тайтл главной страницы из конструктора контроллера Index');

When I call in the router, I have to bind the context to get the necessary data
var index = require('../controllers/index');

router.get('/', index.mainPage.bind(index));

module.exports = router;

Maybe there is some other way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vintage, 2017-03-07
@myspace

Use arrow functions:

router.get('/', ( ...args )=> index.mainPage( ...args ) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question