A
A
Abc Edc2015-04-20 15:26:21
Node.js
Abc Edc, 2015-04-20 15:26:21

How to call dynamic method in child class?

So that the parent method of the class is called on the child
Here is an example of a parent and a child

function MainController () {
    this.model = 'Main';
}
MainController.prototype = {
    constructor: MainController,
    findAll: function (req, res) {
        this.model.find({}, function (err, res) {
            if (err)  res.json({"findAll": err});
            res.json(res);
        });
    },
    findById: function (req, res) {
        var id = new ObjectID(req.params.id);
        this.model.findById(id, function (err, res) {
            if (err)  res.json({"findById": err});
            res.json(res);
        })
    },
    create: function (req, res) {
        var saver = new this.model(req.body);
        saver.save(function (err, res) {
            if (err) throw  err;
            res.json({create: 'yes'});
        });
    },
    update: function (req, res) {

    }
};

module.exports = MainController;

// дочерний
var util = require('util');
var User = require('../models/user').User;
ObjectID = require('mongodb').ObjectID;
var MainController = require('./parent/index')();

var UserController = function () {
    this.model = User;
};

UserController.prototype = {
    prototype: UserController,
    checkAuth: function (req, res, next) {
        if (req.session.user) {
            res.json({login: req.session.user});
        } else {
            next();
        }
    },
    login: function (req, res, next) {
        var mail = req.body.mail;
        var password = req.body.password;
        User.autorize(mail, password, function (err, result, user) {
            if (result.login == 'yes') {
                req.session.user = user._id;
                res.json(result);
            }
            else {
                res.json({login: "no"});
            }
        }
        )
    }
};

util.inherits(UserController, MainController);
module.exports = UserController;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-04-20
@gleber1

What does "dynamic method" mean?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question