forked from matfin/annachristoffer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
202 lines (183 loc) · 4.68 KB
/
routes.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
Router.onRun(function() {
this.next();
});
Router.onBeforeAction(function() {
$('nav, section').removeClass('revealed');
App.currentView.type = false;
App.currentView.id = false;
/**
* Subscribe to simple content published from the server
*/
Meteor.subscribe('categories');
Meteor.subscribe('staticContent');
Meteor.subscribe('pages');
this.next();
});
Router.map(function() {
/**
* General content views with optional content loaded
* if the _content parameter is specified, otherwise
* load the main list page of unfiltered projects.
*/
this.route('content', {
path: '/content/:_page_slug?',
waitOn: function() {
return Meteor.subscribe('pages', this.params._page_slug, App.language);
},
action: function() {
if(this.ready()) {
this.render();
}
else {
this.next();
}
},
data: function() {
if(!this.ready()) {
return;
}
var page = App.models.pages.findOne({}),
alternateLanguages = _.filter(App.languages, function(language) {
return language !== App.language;
});
/**
* If we cannot find the page based on the current slug, then run another
* query to fetch the page in a different language.
*
* This fixes a bug that arises when the user changes the language and
* refreshes the page. We check the slugs of the page in different languages
* and load it.
*/
if(typeof page === 'undefined') {
_.each(alternateLanguages, function(language) {
var query = {};
query['slug.' + language] = this.params._page_slug;
return page = App.models.pages.findOne(query) !== 'undefined';
}.bind(this));
}
App.currentView.type = 'page';
App.currentView.id = page.id;
return page;
},
template: 'template_main',
notFoundTemplate: 'template_notfound',
yieldTemplates: {
'header': {to: 'header'},
'page': {to: 'content'},
'footer': {to: 'footer'}
}
});
/**
* Project list view (all projects) with optional
* filter parameter for showing projects only by
* their category name.
*/
this.route('list', {
path: '/:_category_slug?',
template: 'template_main',
action: function() {
if(this.ready()) {
this.render();
}
else {
this.next();
}
},
waitOn: function() {
return [
Meteor.subscribe('projects'),
Meteor.subscribe('formations'),
Meteor.subscribe('categories')
];
},
data: function() {
if(!this.ready()) {
return;
}
var data = {
projects: App.models.projects.find({}, {sort: {id: 1}}).fetch()
};
App.currentView.type = 'list';
if(this.params._category_slug) {
/**
* Building up the query given the category slug and the language
*/
var query = {},
category,
alternateLanguages = _.filter(App.languages, function(language) {
return language !== App.language;
});
query['slug.' + App.language] = this.params._category_slug;
/**
* Grab the category given the query
*/
category = App.models.categories.findOne(query);
/**
* If we cannot find the category based on the current slug, then run another
* query to fetch the category in a different language.
*
* This fixes a bug that arises when the user changes the language and
* refreshes the page. We check the slugs of the page in different languages
* and load it.
*/
if(typeof category === 'undefined') {
_.each(alternateLanguages, function(language) {
var query = {};
query['slug.' + language] = this.params._category_slug;
category = App.models.categories.findOne(query);
if(typeof category !== 'undefined') return category;
}.bind(this));
}
/**
* Set the App level currentCategoryId
*/
App.currentView.id = category.id;
data.category_id = category.id;
}
else {
/**
* Clear the App level currentCategoryId
*/
App.currentView.id = false;
}
return data;
},
notFoundTemplate: 'template_notfound',
yieldTemplates: {
'header': {to: 'header'},
'list': {to: 'content'},
'footer': {to: 'footer'}
}
});
/**
* Project detail view loaded from project
* slug name.
*/
this.route('detail', {
path: '/project/:_project_slug',
template: 'template_main',
action: function() {
if(this.ready()) {
this.render();
}
else {
this.next();
}
},
waitOn: function() {
return Meteor.subscribe('projects', this.params._project_slug, App.language)
},
data: function() {
if(!this.ready()) {
return;
}
return App.models.projects.findOne({});
},
notFoundTemplate: 'template_notfound',
yieldTemplates: {
'header': {to: 'header'},
'views_detail': {to: 'content'},
'footer': {to: 'footer'}
}
});
});