forked from meteor-blog/meteor-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.coffee
140 lines (118 loc) · 3.17 KB
/
router.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
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
subs = new SubsManager
cacheLimit: 10, # Maximum number of cache subscriptions
expireIn: 5 # Any subscription will be expire after 5 minute, if it's not subscribed again
if Meteor.isClient
Router.onBeforeAction ->
@notFoundTemplate =
if Blog.settings.blogNotFoundTemplate
Blog.settings.blogNotFoundTemplate
else
'blogNotFound'
Iron.Router.hooks.dataNotFound.call @
, only: ['blogShow']
# RSS
Router.route '/rss/posts',
name: 'rss'
where: 'server'
action: ->
@response.write Meteor.call 'serveRSS'
@response.end()
# BLOG INDEX
Router.route '/blog',
name: 'blogIndex'
template: 'custom'
onRun: ->
if not Session.get('postLimit') and Blog.settings.pageSize
Session.set 'postLimit', Blog.settings.pageSize
@next()
waitOn: ->
if (typeof Session isnt 'undefined')
[
subs.subscribe 'posts', Session.get('postLimit')
subs.subscribe 'authors'
]
fastRender: true
data: ->
posts: Post.where {},
sort: publishedAt: -1
# BLOG TAG
Router.route '/blog/tag/:tag',
name: 'blogTagged'
template: 'custom'
waitOn: -> [
subs.subscribe 'taggedPosts', @params.tag
subs.subscribe 'authors'
]
fastRender: true
data: ->
posts: Post.where
tags: @params.tag
,
sort: publishedAt: -1
# SHOW BLOG
Router.route '/blog/:slug',
name: 'blogShow'
template: 'custom'
onRun: ->
Session.set('slug', @params.slug)
@next()
onBeforeAction: ->
if Blog.settings.blogShowTemplate
tpl = Blog.settings.blogShowTemplate
# If the user has a custom template, and not using the helper, then
# maintain the package Javascript.
pkgFunc = Template.blogShowBody.rendered
userFunc = Template[tpl].rendered
if userFunc
Template[tpl].rendered = ->
pkgFunc.call(@)
userFunc.call(@)
else
Template[tpl].rendered = pkgFunc
@next()
action: ->
@render() if @ready()
waitOn: -> [
Meteor.subscribe 'singlePostBySlug', @params.slug
subs.subscribe 'commentsBySlug', @params.slug
subs.subscribe 'authors'
]
data: ->
Post.first slug: @params.slug
# BLOG ADMIN INDEX
Router.route '/admin/blog',
name: 'blogAdmin'
template: 'custom'
onBeforeAction: ->
if Meteor.loggingIn()
return
Deps.autorun () ->
Router.go 'blogIndex' if not Meteor.userId()
Meteor.call 'isBlogAuthorized', (err, authorized) =>
if not authorized
return @redirect('/blog')
@next()
waitOn: ->
[ Meteor.subscribe 'postForAdmin'
Meteor.subscribe 'authors' ]
# NEW/EDIT BLOG
Router.route '/admin/blog/edit/:id',
name: 'blogAdminEdit'
template: 'custom'
onBeforeAction: ->
if Meteor.loggingIn()
return
Deps.autorun () ->
Router.go 'blogIndex' if not Meteor.userId()
Meteor.call 'isBlogAuthorized', @params.id, (err, authorized) =>
if not authorized
return @redirect('/blog')
Session.set 'postId', @params.id
@next() if Session.get("postId").length?
action: ->
@render() if @ready()
waitOn: -> [
Meteor.subscribe 'singlePostById', @params.id
Meteor.subscribe 'authors'
Meteor.subscribe 'postTags'
]