-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouterBase.js
166 lines (149 loc) · 5.38 KB
/
routerBase.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Generated by CoffeeScript 1.3.3
(function() {
define(["Ural/Controllers/controllerBase", "Ural/Modules/pubSub", "hasher", "crossroads", "Ural/text!Ural/Views/Shared/AppLoad.html"], function(controllerBase, pubSub, hasher, crossroads) {
var RouterBase;
RouterBase = (function() {
function RouterBase(controllerDirectory, defaultRoute) {
var _this = this;
this.controllerDirectory = controllerDirectory;
this.defaultRoute = defaultRoute;
this.onLoading();
this._recAction = {
name: null,
obj: null,
action: null
};
this._recHash = null;
this.addRoute('{controller}/index', true, function(ctr, ck) {
return _this.onControllerRouter(ctr, "index", null, ck);
});
this.addRoute('{controller}/item/{idx}', true, function(ctr, idx, ck) {
return _this.onControllerRouter(ctr, "item", idx, null, ck);
});
this.addRoute('{controller}', true, function(ctr, ck) {
return _this.onControllerRouter(ctr, null, null, ck);
});
}
RouterBase.prototype._exec = function(execArgs) {
var args;
args = execArgs.split("!").map(function(m) {
return m.split("/").filter(function(f) {
return f;
});
});
return pubSub.pub("router", "exec", {
args: args
});
};
RouterBase.prototype._hash = function() {
return hasher.getHash().replace(/^([^!]+)\/!\/(.*)/, "$1");
};
RouterBase.prototype._setHashSilently = function(hash) {
hasher.changed.active = false;
hasher.setHash(hash);
return hasher.changed.active = true;
};
RouterBase.prototype.removeRoute = function(route) {
return crossroads.removeRoute(route);
};
RouterBase.prototype.addRoute = function(route, appendExecRoute, callback) {
var execRoute,
_this = this;
crossroads.addRoute(route, callback);
if (appendExecRoute) {
execRoute = "" + route + "/!/{exec*}";
return crossroads.addRoute(execRoute, function() {
var args, exec, hash;
args = _u.argsToArray(arguments);
exec = args[args.length - 1];
hash = _this._hash();
_this._setHashSilently(hash);
if (_this._recHash !== hash) {
args.splice(args.length - 1, 1, function() {
return _this._exec(exec);
});
return callback.apply(_this, args);
} else {
return _this._exec(exec);
}
});
}
};
RouterBase.prototype.getOnLoadingPage = function() {
return "Ural/Views/Shared/AppLoad.html";
};
RouterBase.prototype.onLoading = function() {};
RouterBase.prototype.onNotFound = function() {
controllerBase.ControllerBase.loadView(null, "Ural/Views/Shared/NotFound.html");
return this.onRouteChanged(null, null);
};
RouterBase.prototype.onControllerRouter = function(controller, action, index, callback, persistRoute) {
var capControllerName, controllerName,
_this = this;
this._recHash = this._hash();
if (this._recAction.obj && this._recAction.obj.afterAction) {
this._recAction.obj.afterAction(this._recAction.action);
}
index = this.onParseIndex(controller, action, index);
if (this._recAction.name !== controller) {
if (action == null) {
action = "index";
}
controllerName = "" + controller + "Controller";
capControllerName = "" + (_.str.capitalize(controller)) + "Controller";
return require(["" + this.controllerDirectory + "/" + controllerName], function(controllerModule) {
var ctl;
ctl = eval("new controllerModule." + capControllerName + "()");
ctl[action](index);
_this._recAction = {
name: controller,
obj: ctl,
action: action
};
if (callback) {
callback();
}
if (!persistRoute) {
return _this.onRouteChanged(controller, action);
}
});
} else {
this._recAction.action = action;
this._recAction.obj[action](index);
if (!persistRoute) {
return this.onRouteChanged(controller, action);
}
}
};
RouterBase.prototype.onRouteChanged = function(controller, action) {
if (this.onRouteChangedCallback) {
return this.onRouteChangedCallback(controller, action);
}
};
RouterBase.prototype.startRouting = function() {
var parseHash,
_this = this;
parseHash = function(newHash, oldHash) {
return crossroads.parse(newHash);
};
hasher.initialized.add(parseHash);
hasher.changed.add(parseHash);
hasher.init();
if (!hasher.getHash()) {
hasher.setHash(this.defaultRoute);
}
return crossroads.bypassed.add(function(request) {
console.log(request);
return _this.onNotFound();
});
};
RouterBase.prototype.onParseIndex = function(controller, action, index) {
return index;
};
return RouterBase;
})();
return {
RouterBase: RouterBase
};
});
}).call(this);