1 | /*
|
2 | * Copyright (c) 2018 One Hill Technologies, LLC
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | const Action = require ('./action');
|
18 | const assert = require ('assert');
|
19 |
|
20 | /**
|
21 | * @class ViewAction
|
22 | *
|
23 | * A ViewAction is an action that generates a view based on the content
|
24 | * of the request it is processing.
|
25 | */
|
26 | module.exports = Action.extend ({
|
27 | init () {
|
28 | this._super.call (this, ...arguments);
|
29 |
|
30 | assert (!!this.view, "The 'view(req)' method must be defined by the subclass.");
|
31 | },
|
32 |
|
33 | /**
|
34 | * Get the view template for the action. The view is used to render a
|
35 | * response to the caller.
|
36 | *
|
37 | * @params req The request object
|
38 | * @returns {String|Promise}
|
39 | */
|
40 | view: null,
|
41 |
|
42 | /**
|
43 | * Get the data model used to populate the template.
|
44 | *
|
45 | * @params req The request object
|
46 | * @returns Promise
|
47 | */
|
48 | model (req) {
|
49 | return null;
|
50 | },
|
51 |
|
52 | /**
|
53 | *
|
54 | * @param req The request object
|
55 | * @param res The response object
|
56 | * @returns {Promise}
|
57 | */
|
58 | execute (req, res) {
|
59 | // Get the new and the model from the subclass.
|
60 | let vp = this.view (req);
|
61 | let mp = this.model (req);
|
62 |
|
63 | return Promise.all ([vp,mp]).then (results => {
|
64 | let [view, model] = results;
|
65 |
|
66 | res.render (view, model);
|
67 | });
|
68 | }
|
69 | });
|