1 |
|
2 |
|
3 | var events = require ('events'),
|
4 | urlUtil = require ('url'),
|
5 | util = require ('util');
|
6 |
|
7 |
|
8 | var model = module.exports = function (url, optionalParams) {
|
9 |
|
10 | var self = this;
|
11 |
|
12 | if (url.constructor === String) {
|
13 | try {
|
14 | this.url = urlUtil.parse (url, true);
|
15 | var a = this.url.protocol.length;
|
16 | } catch (e) {
|
17 | self.emit ('error', e);
|
18 | }
|
19 | } else {
|
20 | this.url = url;
|
21 | if (!this.url.protocol) {
|
22 |
|
23 | this.url.protocol = 'http:';
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | this.modelName = this.url.protocol.replace(/(^s|:$|s:$)/g, '');
|
33 |
|
34 |
|
35 | var requiredModel = require ('./'+this.modelName);
|
36 | this.dataSource = new requiredModel (this, optionalParams);
|
37 |
|
38 |
|
39 |
|
40 | this.fetch = function (target) {
|
41 | self.dataSource.fetch(target);
|
42 | }
|
43 |
|
44 | this.store = function (target) {
|
45 | self.dataSource.store(target);
|
46 | }
|
47 |
|
48 | this.stop = function (reason) {
|
49 | if (self.dataSource.stop)
|
50 | self.dataSource.stop (reason);
|
51 | }
|
52 |
|
53 |
|
54 | }
|
55 |
|
56 | util.inherits (model, events.EventEmitter);
|
57 |
|
58 |
|
59 |
|
60 |
|