Remote methods of inherited models do not appear in loopback explorer

I inherited a model in a loop like this, -

{
    "name": "MyModel",
    "base": "AnotherModel",
    "idInjection": false,
    "properties": {
      "value": {
        "type": "String"
      },
      "display": {
        "type": "String"
      }
    },
    "validations": [],
    "relations": {},
    "acls": [],
    "methods": []
}

For now, I can call all deleted methods AnotherModelfrom my file MyModel.js. But remote methods AnotherModeldo not appear in my explorer. How to make all the remote methods of my inherited model appear in Explorer?

+4
source share
2 answers

The reason is that when AnotherModel.remoteMethodhe called, he registered only this remote method for this model, and not models based on this. To call it for all models based on AnotherModel, you can do this:

var originalSetup = AnotherModel.setup;
AnotherModel.setup = function() { // this will be called everytime a 
  // model is extended from this model.

  originalSetup.apply(this, arguments); // This is necessary if your 
  // AnotherModel is based of another model, like PersistedModel.

  this.remoteMethod('yourMethod', {
    ..
  });
};

, , persistedModel , .

, - .

+3

setup() "" AnotherModels. http://loopback.io/doc/en/lb2/Extending-built-in-models.html#setting-up-a-custom-model , : http://loopback.io/doc/en/lb2/Remote-methods#example

: ( )

// AnotherModel.js
module.exports = function (AnotherModel) {
    ...
    // suppose you have two functions to inherit 'function1' and 'function2'
    AnotherModel.prototype.function1 = function() {...}
    AnotherModel.function2 = function() {...}
    // The loopback.Model.extend() function calls setup() 
    // so code you put in setup() will automatically get 
    // executed when the model is created.  
    var originalSetup = AnotherMoldel.setup;
    AnotherMoldel.setup = function() {
        // call the original setup!
        originalSetup.apply(this, arguments);
        // 'this' will points to MyModel during MyModel setup
        // first param should match with function name
        this.remoteMethod('function1', {
            accepts: {...}, 
            returns: {...}, 
            http: {...}});
        this.remoteMethod('function2', {
            accepts: {...}, 
            returns: {...}, 
            http: {...}});
    }
}

API.

, , , ! ( )

0

Source: https://habr.com/ru/post/1598888/


All Articles