-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat.js
98 lines (87 loc) · 3.17 KB
/
sat.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
db = {}, Pages = {};
Sat = { isServer: false, isClient: false };
__ = this.__
Config = this.Config
module = { exports:{} }
__.deepExtend = function (target, source) {
for (var prop in source)
if (prop in target)
__.deepExtend(target[prop], source[prop]);
else
target[prop] = source[prop];
return target;
}
__.capitalize = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
__.isLowerCase = function (char, index) {
return char.charAt(index) >= 'a' && char.charAt(index) <= 'z' ? true : false
}
db.init = function () {
db.collections = Config.collections;
_.each( db.collections, function (collection) {
var Collection = __.capitalize(collection)
if ( 'undefined' === typeof db[ Collection ] )
db[ Collection ] = new Meteor.Collection(collection);
});
}
if ( Meteor.isClient ) {
$( function () {
if ( ! Sat.isClient )
Sat.init();
});
} else if ( Meteor.isServer ) {
Meteor.startup( function() {
if ( ! Sat.isServer )
Sat.init();
});
}
Sat.init = function () {
db.init();
var pages_in_file = module.exports;
if ( Meteor.isServer ) {
Sat.isServer = true;
__.deepExtend( Config, module.exports.ServerConfig );
delete module.exports.ServerConfig;
} else if ( Meteor.isClient ) {
Sat.isClient = true;
Router.configure({ layoutTemplate: 'layout' });
var startup = [];
_.each(_.keys(pages_in_file), function(file) {
if ( __.isLowerCase(file, 0) )
_.extend( Pages, pages_in_file[file] );
if (pages_in_file[file].__events__ && pages_in_file[file].__events__.startup) {
startup.push(pages_in_file[file].__events__.startup);
delete pages_in_file[file].__events__.startup
}
_.each( _.filter( _.keys( pages_in_file[file] ), function (key) {
return ! ( (__.isLowerCase(key, 0) || key.charAt(0) === '_') && __.isLowerCase(key, 1) );
}), function (name) {
delete Pages[name];
});
});
if (startup)
Meteor.startup(function() {
_.each(startup, function(func) { func() });
});
var router_map = {};
_.each(_.keys(Pages), function(name) {
_.each(_.keys(Pages[name]), function(key) {
if (key.substring(0, 2) !== '__' && _.indexOf(Config.templates, key) === -1 )
if (key === 'helpers')
Template[name].helpers( Pages[name].helpers );
else if (key === 'events')
Template[name].events( Pages[name].events );
else if (key === 'router') {
router_map[name] = Pages[name].router;
delete Pages[name].router;
} else
Template[name][key] = Pages[name][key];
});
});
Router.map( function () {
for (var key in router_map)
this.route( key, router_map[key] );
});
}
}