1 |
|
2 | var ModuleDefiner = require('./resourcesDefiner/moduleDefiner');
|
3 | var ControllerDefiner = require('./resourcesDefiner/controllerDefiner');
|
4 | var modelDefiner = require('./resourcesDefiner/modelDefiner');
|
5 | var Server = require('./server');
|
6 |
|
7 |
|
8 | module.exports = Definer
|
9 |
|
10 |
|
11 |
|
12 | function Definer(app){
|
13 | this.app = app
|
14 | this.modules = app.modules
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Definer.prototype.module =function(options){
|
22 | var module = new ModuleDefiner(options);
|
23 | module.app = this.app;
|
24 | this.modules[module.name] = module;
|
25 | return module;
|
26 |
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | Definer.prototype.controller = function(options){
|
33 | var controller = new ControllerDefiner(options);
|
34 |
|
35 |
|
36 | if(!controller.moduleName){
|
37 | controller.moduleName = this.app.defaultModule;
|
38 | }
|
39 |
|
40 | if(!this.modules[controller.moduleName]){
|
41 | console.error(controller.moduleName + ' module does`t exist');
|
42 | throw new Error();
|
43 | }
|
44 |
|
45 | var currentModule = this.modules[controller.moduleName];
|
46 |
|
47 | if(currentModule.controllers[controller.name]){
|
48 |
|
49 | console.error(controller.moduleName + '模块的 ' + controller.name + '控制器 已经被定义,请检查是否重复');
|
50 | throw new Error();
|
51 | }
|
52 |
|
53 | currentModule.controllers[controller.name] = controller;
|
54 | controller.module = currentModule;
|
55 | controller.app = this.app;
|
56 | controller.init();
|
57 | return controller.actionDefiner;
|
58 |
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Definer.prototype.server = function(options){
|
65 | var server = new Server(options,this.app);
|
66 | return server
|
67 | }
|
68 |
|
69 |
|
70 | Definer.prototype.model = function(modelName,dbName,modelInfo,modelOther){
|
71 | if(arguments.length == 3){
|
72 | modelOther = modelInfo;
|
73 | modelInfo = dbName
|
74 | dbName = "default"
|
75 | }
|
76 | var db = this.app.dbs[dbName];
|
77 | var model = db.define(modelName,modelInfo,modelOther);
|
78 | model.__proto__.__proto__= modelDefiner
|
79 |
|
80 | return model
|
81 |
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|