Arne
Arne

Reputation: 6240

Strongloop (loopback) class methods

I have some difficulty understanding how Strongloop models behave. There is a lot of documentation about static and remote methods, but how about general class methods?

Let's say I have a user model, that has a method for showing the full name:

module.exports = function (User) {
    User.name = function () {
        return User.firstname + ' ' + User.lastname;
    }
};

How do I fetch this user and use the method? I would suppose:

var User = app.models.User;

User.findById('559103d66d', function (err, model) {
    console.log(model.name());
});

But apparently, the findById returns a JSON object containing all the properties instead of the actual model. So how does one define and use model methods in Strongloop?

Upvotes: 1

Views: 222

Answers (1)

xangy
xangy

Reputation: 1205

You need to use 'prototype' property of javascript, if your are planning to use name() function on the instance of 'User' model. As follows:

User.prototype.name = function () {
   return this.firstname + ' ' + this.lastname;
}

and you are good to go.

Upvotes: 1

Related Questions