Best way to define custom functions in loopback api

What is the best way to define custom functions in the loopback api that can be used in certain models?

For example, a basic express application may have functions in a subfolder in the root directory, but it is not recommended to do the same in loopback and does not support the loopback method.

Any help would be greatly appreciated.

+4
source share
4 answers

The best way to define the functions in loopback that I found is to use mixins. Here is an example of this.

https://loopback.io/doc/en/lb3/Defining-mixins.html

.json {mixins:yourmixin.js}, .

-2

.

  • :

, .. , , .

+2

, Loopback documentation :

  • - , .
  • - ( /server/boot ), .
  • - .

, , script . , mixins :

mixins , .

, ?

-, Grails, ​​ . , :

  • src/main/ groovy -

, Loopback, src server , , , , . :

const HelperClass = require('../src/HelperClass');
HelperClass.helperFunction()...

, , : src, helpers, support, . common server.

0

Here is an example code to help you move in the right direction.

mynewmodel.js

http: //domain/api/mynewmodel/myfunc.js

function myfunc(data, callback) {
    // ** Put your code here **
    console.log('myfunc');
}

function remoteMethod(model) {

    model.remoteMethod(
        'myfunc',
        {
            http: { verb: 'post' },
            accepts: { arg: 'data', type: 'data' },
            returns: [
                { arg: 'returndata', type: 'data' }
            ]
        }
    )
}

UPDATE Typically, your js files are included in the total number / models

-2
source

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


All Articles