【问题标题】:Loopback models in ES6ES6 中的环回模型
【发布时间】:2016-08-17 21:29:21
【问题描述】:

我是 loopback.js 的新手。我想对类使用 ES6 语法。

如何使用loopback定义模型的方式来实现?

module.exports = function(MyModel) {
  MyModel.on('dataSourceAttached', function(obj){
    var find = MyModel.find;
    MyModel.find = function(filter, cb) {
      return find.apply(this, arguments);
    };
  });
};

【问题讨论】:

    标签: javascript node.js ecmascript-6 loopbackjs


    【解决方案1】:

    https://github.com/NextFaze/Loopback-Style-Guide#custom-model-methods 的一些想法 我在 ES6 中为管理模型创建了一个引导类。我在 ES5 和 ES6 中创建了 2 个示例。

    ES5 模型示例

    module.exports = function(Sbif) {
    
      var request = require('request-promise'),
          token    = process.env.SBIF_TOKEN_URL,
          api_url  =  process.env.SBIF_API_URL;
    
      // UTM ---------------------------------------------
      Sbif.utm = function(params, cb) {
        makeRequest('utm',{}).then(function(res) {
          cb(null, JSON.parse(res).UTMs[0]);
        });
      };
    
      Sbif.remoteMethod('utm',{
        accepts: {arg: 'params', type: 'string'},
        returns: {arg: 'utm', type: 'string'},
        http: {path: '/utm', verb: 'get' }
      });
    
      // IPC ---------------------------------------------
      Sbif.ipc = function(params, cb) {
        makeRequest('ipc', {}).then(function(res) {
          cb(null, JSON.parse(res).IPCs[0]);
        });
      };
    
      Sbif.remoteMethod('ipc', {
        accepts: {arg: 'params', type: 'string'},
        returns: {arg: 'ipc', type: 'string'},
        http: {path: '/ipc'}
      });
    
      // EURO --------------------------------------------
      Sbif.euro = function(params, cb) {
        makeRequest('euro', {}).then(function(res) {
          cb(null, JSON.parse(res).Euros[0]);
        });
      };
    
      Sbif.remoteMethod('euro', {
        accepts: {arg: 'params', type: 'string'},
        returns: {arg: 'euro', type: 'string'},
        http: {path: '/euro'}
      });
    
      // Dolár -------------------------------------------
      Sbif.dolar = function(params, cb) {
        makeRequest('dolar',{}).then(function(res) {
          cb(null,JSON.parse(res).Dolares[0]);
        });
      };
    
      Sbif.remoteMethod('dolar', {
        accepts: {arg: 'params', type: 'string'},
        returns: {arg: 'dolar', type: 'string'},
        http: {path: '/dolar'}
      });
    
    
      // UF ----------------------------------------------
      Sbif.uf = function(params, cb) {
        makeRequest('uf',{}).then(function(res) {
          cb(null, JSON.parse(res).UFs[0]);
        });
      };
    
      Sbif.remoteMethod( 'uf',{
        accepts: {arg: 'params', type: 'string'},
        returns: {arg: 'uf', type: 'string'},
        http: {path: '/uf', verb: 'get' }
      });
    
    
      // SBIF Token and URL
      function makeRequest(indicador, parametros){
        return request(buildUrl(indicador, parametros));
      }
    
      function buildUrl(indicador, parametros){
        var url = api_url + indicador;
        if(parametros.length > 0){
          parametros.forEach((parametros) {
            url += '/' + parametros; 
          });
        }
        return url + "?apikey=" + token + "&formato=json";
      }
    
    
    };
    

    ES6 模型示例

    import request from 'request-promise';
    
    var token    = process.env.SBIF_TOKEN_URL,
        api_url  =  process.env.SBIF_API_URL;
    
    class SbifApi{
      constructor(SbifModel){
        //map remote methods to class methods. Set an extra this using the .bind function
        SbifModel.uf = this.getData.bind(SbifModel, 'uf');
        SbifModel.utm = this.getData.bind(SbifModel, 'utm');
        SbifModel.ipc = this.getData.bind(SbifModel, 'ipc');
        SbifModel.dolar = this.getData.bind(SbifModel, 'dolar');
        SbifModel.euro = this.getData.bind(SbifModel, 'euro');
    
        //Set remotes
        this.setRemotes(SbifModel, {name: 'uf', path: '/uf', root: 'uf'});
        this.setRemotes(SbifModel, {name: 'utm', path: '/utm', root: 'utm'});
        this.setRemotes(SbifModel, {name: 'ipc', path: '/ipc', root: 'ipc'});
        this.setRemotes(SbifModel, {name: 'dolar', path: '/dolar', root: 'dolar'});
        this.setRemotes(SbifModel, {name: 'euro', path: '/euro', root: 'euro'});
    
        return SbifModel;
      }
      setRemotes(model, remoteMethod){
        let remoteOpts = {
          accepts: {
            arg: 'params', 
            type: 'string'
          },
          returns: {
            arg: remoteMethod.root || remoteMethod.name,
            type: 'string'
          },
          http: {
            path: remoteMethod.path || '/' + remoteMethod.name, 
            verb: 'get' 
          }
        };
        //Set the model remote methodnpm
        model.remoteMethod(remoteMethod.name, remoteOpts);
      }
      getData(type, params, callback){
        request(`${api_url}/${type}?apikey=${token}&formato=json`).then((res) => {
    
          let data = JSON.parse(res);
    
          switch(type){
            case 'uf': 
              data = data.UFs[0]; 
              break;
            case 'utm': 
              data = data.UTMs[0]; 
              break;
            case 'ipc': 
              data = data.IPCs[0]; 
              break;
            case 'dolar': 
              data = data.Dolares[0]; 
              break;
            case 'euro': 
              data = data.Euros[0]; 
              break;
          }
          callback(null, data);
        });
      }
    }
    export default function(Model) { new SbifApi(Model); }
    

    【讨论】:

      猜你喜欢
      • 2018-09-28
      • 2016-07-01
      • 2015-03-21
      • 2016-06-26
      • 2016-05-08
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多