Skip to content

Latest commit

 

History

History
68 lines (50 loc) · 2.8 KB

Define-a-remote-hook.md

File metadata and controls

68 lines (50 loc) · 2.8 KB
title lang layout toc keywords tags sidebar permalink summary
Define a remote hook
ja
page
false
LoopBack
getting_started
ja_lb3_sidebar
/doc/ja/lb3/Define-a-remote-hook.html
A remote hook is a function that's executed before or after a remote method.

{% include content/ja/gs-prereqs.html two="true" %}

{% include note.html content=" If you followed the previous step in the tutorial, go to Introducing remote hooks.

If you're just jumping in, follow the steps below to catch up... " %}

Get the app (in the state following the last article) from GitHub and install all its dependencies:

$ git clone https://github.com/strongloop/loopback-getting-started-intermediate.git
$ cd loopback-getting-started-intermediate
$ git checkout step4
$ npm install

Introducing remote hooks

A remote hook is simply a function that gets executed before or after a remote method (either a custom remote method or a built-in CRUD method).   In this example, you're going to define a remote hook that is called whenever the create() method is called on the Review model; that is, when a new review is created.

You can define two kinds of remote hooks:

  • beforeRemote() runs before the remote method.
  • afterRemote() runs after the remote method.

In both cases, you provide two arguments: a string that matches the remote method to which you want to "hook" your function, and a callback function.  Much of the power of remote hooks is that the string can include wildcards, so it is triggered by any matching method.

{% include note.html content=" LoopBack also provides operation hooks, functions that are executed before or after models perform backend operations such as creating, saving, and updating model data, regardless of how those operations are invoked. In contrast, a remote hook is called only when the exact method you specify is invoked. " %}

Create the remote hook

Here, you're going to define a remote hook on the review model, specifically Review.beforeRemote.

Modify common/models/review.js, and add the following code:

{% include code-caption.html content="common/models/review.js" %}

module.exports = function(Review) {
  Review.beforeRemote('create', function(context, user, next) {
    context.args.data.date = Date.now();
    context.args.data.publisherId = context.req.accessToken.userId;
    next();
  });
};

This function is called before a new instance of the Review model is created.  The code:

  • Inserts the publisherId using the access token attached to the request.
  • Sets the date of the review instance to the current date.

{% include next.html content="Continue to Create AngularJS client." %}