Skip to content

Instantly share code, notes, and snippets.

@HynesIP
Forked from LeCoupa/iron-router-cheatsheet.coffee
Last active August 29, 2015 14:07
Show Gist options
  • Save HynesIP/efad9d55e1257a0faa31 to your computer and use it in GitHub Desktop.
Save HynesIP/efad9d55e1257a0faa31 to your computer and use it in GitHub Desktop.
var AdminController, PostsEditController;
Router.configure({
layoutTemplate: 'Main',
loadingTemplate: 'Loading',
notFoundTemplate: 'NotFound',
load: function() {
$('html, body').animate({
scrollTop: 0
}, 400);
return $('.content').hide().fadeIn(1000);
},
waitOn: function() {
return Meteor.subscribe('recordSetThatYouNeedNoMatterWhat');
}
});
Router.map(function() {
this.route('Homepage', {
path: '/'
});
this.route('Contact', {
layoutTemplate: 'Layout',
loadingTemplate: 'Loading',
notFoundTemplate: 'NotFound',
template: 'Contact',
path: '/contact',
where: 'client',
yieldTemplates: {
'MyAsideTemplate': {
to: 'aside'
},
'MyFooter': {
to: 'footer'
}
}
});
this.route('PostShow', {
action: function() {
this.render();
this.render('templateName');
return this.render('templateName', {
to: 'region'
});
},
controller: 'CustomController',
data: function() {
return Posts.findOne(this.params._id);
},
load: function() {
return console.log('runs just once when the route is first loaded.');
},
onBeforeAction: function() {
var post;
post = this.getData();
return console.log('runs before the action function (possibly many times if reactivity is involved).');
},
onAfterAction: function() {
return console.log('runs after the action function (also reactively).');
},
path: '/posts/:_id',
unload: function() {
return console.log('runs just once when you leave the route for a new route.');
},
waitOn: function() {
return Meteor.subscribe('post', this.params._id);
}
});
this.route('TwoSegments', {
path: '/posts/:paramOne/:paramTwo'
});
this.route('Globbing', {
path: '/posts/*'
});
this.route('NamedGlobbing', {
path: '/posts/:file(*)'
});
this.route('RegularExpressions', {
path: /^\/commits\/(\d+)\.\.(\d+)/
});
return this.route('ServerRoute', {
action: function() {
var filename;
filename = this.params.filename;
this.response.writeHead(200, {
'Content-Type': 'text/html'
});
return this.response.end('hello from server');
},
where: 'server'
});
});
Router.onRun(function() {
return console.log('this happens once only when the route is loaded.');
});
Router.onData(function() {
return console.log('runs reactively whenever the data changes.');
});
Router.onBeforeAction(function() {
return console.log('runs reactively before the action.');
});
Router.onAfterAction(function() {
return console.log('runs reactively before the action.');
});
Router.onStop(function() {
return console.log('runs once when the controller is stopped, like just before a user routes away.');
});
AdminController = RouteController.extend({
onBeforeAction: function() {}
});
PostsEditController = AdminController.extend({
waitOn: function() {
return Meteor.subscribe('adminPost');
}
});
Router.map(function() {
return this.route(postsEdit, {
path: '/posts/:_id/edit'
});
});
Router.routes['Homepage'];
Router.routes['PostShow'].path({
_id: 1
});
Router.routes['PostShow'].path({
_id: 1
}, {
query: 'sort_by=created_at',
hash: 'someAnchorTag'
});
Router.go('Homepage');
Router.go('PostShow', {
_id: 7
});
Router.path('Homepage');
Router.current().path;
//- By default, the router renders the current template directly into the body.
//- If you'd like to share common HTML between routes, you can create your own layout:
//- More: https://github.com/EventedMind/iron-router#layouts--rendering
template(name="Layout")
aside
{{> yield region='aside'}}
.content
{{> yield}}
footer
{{> yield region='footer'}}
//- To use a route in your app, you can use the {{pathFor}} handlebars helper:
//- More: https://github.com/EventedMind/iron-router#using-routes
template(name="Header")
//- set the url to '/'
a(href="{{pathFor 'Homepage'}}") Return to Homepage
//- given a context of {_id: 1} this will render '/posts/1'
a(href="{{pathFor 'PostShow'}}") Post Show
//- given a context of {_id: 1} this will render '/posts/1?sort_by=created_at#someAnchorTag'
a(href="{{pathFor 'PostShow' query='sort_by=created_at' hash='someAnchorTag'}}") Post Show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment