What's the best way to override post method ?

37 показвания
Преминаване към първото непрочетено съобщение

Rémi Flipo

непрочетено,
16.08.2016 г., 6:08:0116.08.16 г.
до LoopbackJS
I have found 3 ways so far

1.
Model.on('attached',function(){
      Model.create = function(data, callback){
          ...
      }
  });

2. a before save hook, using context instance

Model.observe('before save', function doSth(ctx, next) {
    var model = ctx.instance;
    if (model) {...

3. Disable the post remote method and add a new one :

Model.disableRemoteMethod('create', true);
--
Model.remoteMethod(
    'myNewPost',
      {
       ...,
       http: {path: '/', verb: 'post'},
    }
);

I prefer solutions 1/2 in terms of readability/redundancy but maybe 3 is better for performance ?

Akram Shehadi

непрочетено,
16.08.2016 г., 10:58:3116.08.16 г.
до LoopbackJS
Why would 3 be better for performance?  because the original POST method in 1/2 still gets executed regardless of what you do on your override?

Rémi Flipo

непрочетено,
16.08.2016 г., 11:31:5416.08.16 г.
до LoopbackJS
3 and 1 sorry,

because the hook function (2) is called anytime i save my model (not only on my create remote method) !
Отговор до всички
Отговор до автора
Препращане
0 нови съобщения