UNPKG

2.35 kBJavaScriptView Raw
1/******************************************************************************
2 * Lemonade JS
3 * https://lemonadejs.com
4 *
5 * ----
6 *
7 * Instance
8 *
9 * Creates a context for loading models / controllers / intervals and setting
10 * different properties; it's not shared between requests and it's meant to
11 * clear all used memory by a request on request end
12 *
13 * Has some basic abstract methods which instance creators should overload
14 *
15 *****************************************************************************/
16
17function Instance () {
18 this.id = 'generic-' + Math.floor(Math.random() * 1000);
19 this.starttime = new Date();
20 this.endtime = null;
21}
22
23/**
24 * Dependencies
25 */
26var include = require('./include.js');
27
28/**
29 * Instance end
30 * @abstract
31 */
32Instance.prototype.end = function(error, data) {};
33
34/**
35 * Instance echo
36 * @abstract
37 */
38Instance.prototype.echo = function(data) {};
39
40/**
41 * Instance error
42 * @abstract
43 */
44Instance.prototype.error = function(data) {};
45
46/**
47 * Include a controller
48 * @param {String} path
49 * @return {function}
50 */
51Instance.prototype.controller = function(path) {
52 return include(
53 this.kernel.appdir
54 + this.kernel.config._KERNEL_DIR_CONTROLLERS + '/'
55 + path
56 ,this ,this.kernel
57 );
58};
59
60/**
61 * Include an interval
62 * @param {String} path
63 * @return {function}
64 */
65Instance.prototype.interval = function(path) {
66 return include(
67 this.kernel.appdir
68 + this.kernel.config._KERNEL_DIR_INTERVALS + '/'
69 + path
70 ,this ,this.kernel
71 );
72};
73
74/**
75 * Include a model
76 * @param {String} path
77 * @return {function}
78 */
79Instance.prototype.model = function(path) {
80 return include.call(this,
81 this.kernel.appdir
82 + this.kernel.config._KERNEL_DIR_MODELS + '/'
83 + path
84 ,this ,this.kernel
85 );
86};
87
88/**
89 * Include a view
90 * @param {string} path
91 * @param {object} vars
92 * @return {object|string}
93 */
94Instance.prototype.view = function(path, vars) {
95 var view = this.kernel.include(
96 this.kernel.kerneldir + 'instance/view'
97 ).new();
98 path = this.kernel.appdir + 'views/' + path;
99 if (!vars) {
100 return view.load(path);
101 } else {
102 view.load(path);
103 return view.parse(vars);
104 }
105};
106
107/**
108 * Export
109 */
110exports = module.exports = Instance;
\No newline at end of file