Skip to content

Instantly share code, notes, and snippets.

@alanning
Created February 3, 2013 22:53
Show Gist options
  • Save alanning/4704096 to your computer and use it in GitHub Desktop.
Save alanning/4704096 to your computer and use it in GitHub Desktop.
meteor-mini-pages - Before callback hack to transparently render a different template than originally defined.
var beforeFilters = {
authenticate: function (context, page) {
var user
if (Meteor.loggingIn()) {
console.log('authenticate: loading')
//context.redirect( Meteor.loadingPath() )
if (!page.origTemplate) {
page.origTemplate = page.templateName
}
page.templateName = 'loading'
context.loading = true
context.handled = true
return
} else {
delete context.loading
user = Meteor.user()
if (!user) {
console.log('authenticate: login')
//context.redirect( Meteor.loginPath() )
if (!page.origTemplate) {
page.origTemplate = page.templateName
}
page.templateName = 'login'
context.loggingIn = true
context.handled = true
return
}
delete context.loggingIn
if (!emailVerified(user)) {
console.log('authenticate: awaitingVerification')
//context.redirect( Meteor.awaitingVerificationPath() )
if (!page.origTemplate) {
page.origTemplate = page.templateName
}
page.templateName = 'awaitingVerification'
context.handled = true
return
}
}
console.log('authenticate: passed')
if (page.origTemplate) {
// restore original template
console.log('authenticate: restoring original template - ' + page.origTemplate)
page.templateName = page.origTemplate
delete page.origTemplate
}
}
}
@cmather
Copy link

cmather commented Feb 4, 2013

cc @pkaushik

Thanks for showing me this use case @alanning. In the next release I think I need to address this because the authorize() case falls apart a little with the current design. I'm thinking it would be nice to have something like:

function filter (context, page) {
  if (authorized()) page.useTemplate("someTemplate");
  else page.useTemplate("someOtherTemplate"); // or use default
}

I'd also like to make it easier to specify global before filters so you don't have to type it for every route. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment