/
appBase.js
66 lines (56 loc) · 2.41 KB
/
appBase.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
/*---------------------------------------------------------*/
/* The routing strategy */
(function (app, $) {
var st = {
strategies: {
mvc: {
defaultController: 'home',
defaultAction: 'index',
controller: function controllerFromUrl() {
// match urls in the form ../controllerName/actionName or ../controllerName
var ctl = document.location.href.match(/http[s]?:\/\/[\w\:\.\-\d]+\/([\w\d\-]+)[\/|$]?/i);
return ctl ? ctl[1].toLowerCase() : this.defaultController;
},
action: function actionFromUrl() {
// match urls in the form ../controllerName/actionName or ../controllerName
var act = document.location.href.match(/http[s]?:\/\/[\w\:\.\-\d]+\/[\w\d\-]+\/([\w\d\-]+)/i);
return act ? act[1].toLowerCase() : this.defaultAction;
}
}
}
};
window.myApplication = $.extend(app, st);
})(window.myApplication || {}, jQuery);
/*--------------------------------------------*/
/* The basic routing object */
(function (app, $) {
var opts = {
routingStrategy: app.strategies.mvc
};
var theApp = {
activeController: undefined,
controllers: {},
run: function () {
this.initController();
this.initAction();
},
initController: function () {
var activeControllerName = opts.routingStrategy.controller(); /*getCurrentControllerName*/
if (activeControllerName && this.controllers[activeControllerName]) {
this.activeController = new this.controllers[activeControllerName]();
this.activeController.init();
}
},
initAction: function () {
var activeActionName = opts.routingStrategy.action(); /*getCurrentActionName*/
if (activeActionName && this.activeController && this.activeController[activeActionName]) {
this.activeController[activeActionName]();
}
}
};
/* pusblish the routing object */
window.myApplication = $.extend(app, theApp);
})(window.myApplication || {}, jQuery);
$(function () {
myApplication.run();
});