-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
106 lines (90 loc) · 2.53 KB
/
app.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
98
99
100
101
102
103
104
105
106
// Load our main app functions into an app variable
(function app(options){
// Set a version
this.version = "1.0";
// handle options we've been passed.
this.options = options || {};
this.options.panel = this.options.panel || 'panel';
// Set an error handler.
this.error = function(title,msg){
if(this.options.die){
// we're not catching because we want the script to die.
throw new Error(title+": "+msg);
} else if(this.options.log){
console.log(title+": "+msg);
} else { return true; }
}
// Test our required plugins.
if(typeof _ == 'undefined' || typeof Grapnel == 'undefined'){
throw new Error('one or more of the required libs is missing.');
}
// Set a function to allow us to extend our class
this.extend = function(a,b){
this[a] = b;
// test our requirements
if(typeof this[a].require == 'object' && this[a].require.length > 0){
for(var subject in this[a].require){
if(typeof this[this[a].require[subject]] == 'undefined'){
this.error('Module Error','The required module '+this[a].require[subject]+' was requested but not found.');
// make sure we actually dump out, in case the throw is suppressed.
return null;
}
}
}
// Give ourselves a constructor.
if(typeof this[a]._construct == "function"){
this[a]._construct();
}
}
// Allow the stashing of values on our object. Obviously this may replacee existing methods.
this.set = function(a,b){
this[a] = b;
}
this.render = function(content){
document.getElementById(this.options.panel).innerHTML = content;
}
window.app = this;
})({die: 1});
// Add out view module
// A a simple views class, using _ templates.
// ToDo:- Fetch templates from file.
// ToDo:- Bind a view to a model
app.extend("views",{
_views: [],
add: function(name, html){
this._views[name] = _.template(html);
},
show: function(name, obj){
return this._views[name](obj);
}
});
// Add our routes module
// ToDo:- Add route mapping
app.extend("controllers",{
router: new Grapnel().router(),
register: function(obj){
this.router.get(obj.controller, obj.callback);
}
});
// Add out models module
app.extend("models",{
collection: {},
add: function(name, model){
this.collection[name] = model;
return this;
},
remove: function(name, model){
delete this.collection[model];
return this;
},
set: function(model,data){
for(var a in data){
if(typeof this.collection[model][a] != 'undefined'){
this.collection[model][a] = data[a];
}
}
},
get: function(model){
return this.collection[model];
}
});