forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.js
More file actions
69 lines (62 loc) · 1.46 KB
/
services.js
File metadata and controls
69 lines (62 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'
/**
* adonis-framework
* Copyright(c) 2015-2016 Harminder Virk
* MIT Licensed
*/
const co = require('co')
const Ioc = require('adonis-fold').Ioc
/**
* yield tag for views
* @class
*/
function ViewsYield () {
this.tags = ['yield']
/**
* nunjucks standard parser it looks for yield
* tag and returns everything inside it.
*
* @param {Object} parser
* @param {Function} nodes
* @param {Object} lexer
* @return {Object}
*
* @public
*/
this.parse = function (parser, nodes) {
var tok = parser.nextToken()
var args = parser.parseSignature(null, true)
parser.advanceAfterBlockEnd(tok.value)
return new nodes.CallExtensionAsync(this, 'run', args)
}
/**
* nunjucks run function, it will run this Function
* everytime it finds an execution block with yield tag
*
* @param {Object} context
* @param {Object} injections
* @param {Function} callback
*
* @public
*/
this.run = function (context, injections, callback) {
var keys = Object.keys(injections)
var index = keys[0]
var method = injections[index]
co(function * () {
return yield method
})
.then(function (response) {
context.ctx[index] = response
callback()
})
.catch(function (error) {
callback(error)
})
}
}
exports = module.exports = function (env) {
env.addExtension('yield', new ViewsYield())
env.addGlobal('make', Ioc.make)
env.addGlobal('use', Ioc.use)
}