-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouterBase.coffee
96 lines (81 loc) · 3.51 KB
/
routerBase.coffee
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
define ["Ural/Controllers/controllerBase", "Ural/Modules/pubSub", "hasher", "crossroads", "Ural/text!Ural/Views/Shared/AppLoad.html"],
(controllerBase, pubSub, hasher, crossroads) ->
class RouterBase
constructor: (@controllerDirectory, @defaultRoute) ->
@onLoading()
@_recAction = name : null , obj : null, action : null
@_recHash = null
@addRoute '{controller}/index', true, (ctr, ck) =>
@onControllerRouter ctr, "index", null, ck
@addRoute '{controller}/item/{idx}', true, (ctr, idx, ck) =>
@onControllerRouter ctr, "item", idx, null, ck
@addRoute '{controller}', true, (ctr, ck) =>
@onControllerRouter ctr, null, null, ck
_exec: (execArgs) ->
args = execArgs.split("!").map (m) -> m.split("/").filter((f) -> f)
pubSub.pub "router", "exec", args : args
_hash: ->
hasher.getHash().replace(/^([^!]+)\/!\/(.*)/, "$1")
_setHashSilently: (hash) ->
hasher.changed.active = false
hasher.setHash hash
hasher.changed.active = true
removeRoute: (route) ->
crossroads.removeRoute route
addRoute: (route, appendExecRoute, callback) ->
crossroads.addRoute route, callback
if appendExecRoute
execRoute = "#{route}/!/{exec*}"
crossroads.addRoute execRoute, =>
args = _u.argsToArray arguments
exec = args[args.length - 1]
hash = @_hash()
@_setHashSilently hash
if @_recHash != hash
args.splice args.length - 1, 1, => @_exec exec
callback.apply @, args
else
@_exec exec
getOnLoadingPage: -> "Ural/Views/Shared/AppLoad.html"
onLoading: ->
#controllerBase.ControllerBase.loadView null, @getOnLoadingPage()
onNotFound: ->
controllerBase.ControllerBase.loadView null, "Ural/Views/Shared/NotFound.html"
@onRouteChanged null, null
onControllerRouter: (controller, action, index, callback, persistRoute) ->
@_recHash = @_hash()
if @_recAction.obj and @_recAction.obj.afterAction
@_recAction.obj.afterAction @_recAction.action
index = @onParseIndex controller, action, index
if @_recAction.name != controller
action ?= "index"
controllerName = "#{controller}Controller"
capControllerName = "#{_.str.capitalize controller}Controller"
require ["#{@controllerDirectory}/#{controllerName}"], (controllerModule) =>
ctl = eval "new controllerModule.#{capControllerName}()"
ctl[action] index
@_recAction = name : controller, obj : ctl, action : action
if callback then callback()
if !persistRoute
@onRouteChanged controller, action
else
@_recAction.action = action
@_recAction.obj[action] index
if !persistRoute
@onRouteChanged controller, action
onRouteChanged: (controller, action) ->
if @onRouteChangedCallback then @onRouteChangedCallback controller, action
startRouting: ->
#setup hasher
parseHash = (newHash, oldHash) -> crossroads.parse newHash
hasher.initialized.add(parseHash); #parse initial hash
hasher.changed.add(parseHash); #parse hash changes
hasher.init(); #start listening for history change
if !hasher.getHash()
hasher.setHash @defaultRoute
crossroads.bypassed.add (request) =>
console.log request
@onNotFound()
#onParseIndex: (controller, action, index) -> parseInt index
onParseIndex: (controller, action, index) -> index
RouterBase : RouterBase