Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. WELCOME OUR MANIFESTO OUR PRODUCTS JOURNAL CONTACT US
  3.  LATEST
  4.  METEOR
  5.  NODE.JS
  6.  NODE-WEBKIT
  7.  JAVASCRIPT
  8.  CASPERJS
  9.  GHOST
  10.  CHEATSHEETS
  11.  LIKE US
  12.  FOLLOW US
  13.  ADD US
  14.  WORK WITH US
  15.  WRITE WITH US
  16. METEOR #11: IRON ROUTER CHEATSHEET
  17. {#author.name#}"s picture BY JULIEN LE COUPANEC  19 DAYS AGO  METEOR, CHEATSHEET
  18.  
  19. To celebrate the first anniversary of Iron Router, we made a little CheatSheet to summarize all the available features: configuration, mapping, hooks, route controllers, helpers. Everything is there!
  20. You can star or fork the gist here.
  21. # A little Meteor CheatSheet about Iron-Router.
  22. # Gist: https://gist.github.com/LeCoupa/59738c28fdc9085b9212
  23.  
  24.  
  25. meteor add iron:router
  26. meteor update iron:router
  27.  
  28.  
  29. # Iron Router > Configuration
  30.  
  31.  
  32. Router.configure
  33.  layoutTemplate: 'Main'
  34.  loadingTemplate: 'Loading'
  35.  notFoundTemplate: 'NotFound'
  36.  
  37.  load: ->
  38.    $('html, body').animate({ scrollTop: 0 }, 400)
  39.    $('.content').hide().fadeIn(1000)
  40.  
  41.  waitOn: ->
  42.    return Meteor.subscribe('recordSetThatYouNeedNoMatterWhat')
  43.  
  44.  
  45. # Iron Router > Mapping
  46.  
  47.  
  48. Router.map ->
  49.  # If you don't provide a template, the route will assume the template name is the same as the route name.
  50.  @route 'Homepage',
  51.    path: '/'
  52.  
  53.  # Where it makes sense, options can be set globally via Router.configure()
  54.  @route 'Contact',
  55.    layoutTemplate: 'Layout'      # the layout template to render.
  56.    loadingTemplate: 'Loading'    # the template used by the loading hook.
  57.    notFoundTemplate: 'NotFound'  # the template used by the dataNotFound hook -- renders if the data() function returns something falsey.
  58.    template: 'Contact'           # the template to render. We've seen that by default this is just the name of the route.
  59.    path: '/contact'              # the route will map to the path /contact
  60.    where: 'client'               # whether this route runs on the client or the server (client by default)
  61.    yieldTemplates:
  62.      'MyAsideTemplate': {to: 'aside'}
  63.      'MyFooter': {to: 'footer'}
  64.  
  65.  # One Required Parameter
  66.  @route 'PostShow',
  67.    # Using a Custom Action Function
  68.    action: ->
  69.      # this: instance of RouteController
  70.      # access to: this.params, this.wait, this.render, this.redirect
  71.      @render()
  72.      @render('templateName')
  73.      @render('templateName', {to: 'region'})
  74.  
  75.    # When we do not specify a controller, run the route will look for a global object named "PostShowController", after the name of the route
  76.    # We can change this behavior by providing a controller option to the route like so:
  77.    controller: 'CustomController'
  78.  
  79.    data: ->
  80.      # The data value can either be an object or a function that gets evaluated later (when your route is run).
  81.      # `this` is an instance of a RouteController in the data function above.
  82.      return Posts.findOne(@params._id)
  83.  
  84.    load: ->
  85.      # this doesn't run again if your page reloads via hot-code-reload,
  86.      # so make sure any variables you set will persist over HCR (for example Session variables).
  87.      console.log 'runs just once when the route is first loaded.'
  88.  
  89.    onBeforeAction: ->
  90.      # The value can be a function or an array of functions which will be executed in the order they are defined.
  91.      # You can access the current data context using the getData function inside of any of your route functions.
  92.      post = @getData()
  93.      console.log 'runs before the action function (possibly many times if reactivity is involved).'
  94.  
  95.    onAfterAction: ->
  96.      # The value can be a function or an array of functions which will be executed in the order they are defined.
  97.      console.log 'runs after the action function (also reactively).'
  98.  
  99.    # matches: '/posts/{x}'
  100.    # to set an optional paramater, :optionalParam?
  101.    path: '/posts/:_id'
  102.  
  103.    unload: ->
  104.      # This is called when you navigate to a new route
  105.      console.log 'runs just once when you leave the route for a new route.'
  106.  
  107.    waitOn: ->
  108.      # The waitOn function can return any object that has a ready method.
  109.      # It can also return an array of these objects if you'd like to wait on multiple subscriptions.
  110.      # If you've provided a loadingTemplate, the default action will be to render that template.
  111.      return Meteor.subscribe('post', @params._id)
  112.  
  113.  # Multiple Parameters
  114.  @route 'TwoSegments',
  115.    # matches: '/posts/1/2'
  116.    # matches: '/posts/3/4'
  117.    path: '/posts/:paramOne/:paramTwo'
  118.  
  119.  # Anonymous Parameter Globbing
  120.  @route 'Globbing',
  121.    # matches: '/posts/some/arbitrary/path'
  122.    # matches: '/posts/5'
  123.    # route globs are available
  124.    path: '/posts/*'
  125.  
  126.  # Named Parameter Globbing
  127.  @route 'NamedGlobbing',
  128.    # matches: '/posts/some/arbitrary/path'
  129.    # matches: '/posts/5'
  130.    # stores result in this.params.file
  131.    path: '/posts/:file(*)'
  132.  
  133.  # Regular Expressions
  134.  @route 'RegularExpressions',
  135.    # matches: '/commits/123..456'
  136.    # matches: '/commits/789..101112'
  137.    path: /^\/commits\/(\d+)\.\.(\d+)/
  138.  
  139.  # When you define a server route (via where: 'server'), you need to define the action function, and use in a fairly simplistic way, much like express.
  140.  # The render method is not available. Also, you cannot waitOn subscriptions or call the wait method on the server.
  141.  # Server routes get the bare request, response, and next properties of the Connect request, as well as the params object just like in the client.
  142.  @route 'ServerRoute',
  143.    action: ->
  144.      filename = @params.filename;
  145.  
  146.      @response.writeHead(200, {'Content-Type': 'text/html'});
  147.      @response.end('hello from server');
  148.  
  149.    where: 'server'
  150.  
  151.  
  152. # Iron Router > Hooks
  153. # For all the hooks below, the second argument can be except -- a list of routes to not apply to,
  154. # or only -- a limited set of routes to match.
  155.  
  156.  
  157. Router.onRun ->
  158.  # if the page hot code reloads, the onRun hook will not re-run.
  159.  console.log 'this happens once only when the route is loaded.'
  160.  
  161. Router.onData ->
  162.  console.log 'runs reactively whenever the data changes.'
  163.  
  164. Router.onBeforeAction ->
  165.  console.log 'runs reactively before the action.'
  166.  
  167. Router.onAfterAction ->
  168.  console.log 'runs reactively before the action.'
  169.  
  170. Router.onStop ->
  171.  console.log 'runs once when the controller is stopped, like just before a user routes away.'
  172.  
  173.  
  174. # Iron Router > Route Controllers
  175.  
  176.  
  177. AdminController = RouteController.extend
  178.  # We can define almost all of the same options on our RouteController as we have for our routes.
  179.  # Note that `where` is not available on controllers, only in Router.map.
  180.  onBeforeAction: ->
  181.    # a user filter to control access?
  182.  
  183. PostsEditController = AdminController.extend
  184.  waitOn: ->
  185.    return Meteor.subscribe('adminPost', ...)
  186.  
  187. Router.map ->
  188.  # this will automatically match the `PostsEditController` thanks to the name.
  189.  @route(postsEdit, {path: '/posts/:_id/edit'})
  190.  
  191.  
  192. # Iron Router > Helpers
  193.  
  194.  
  195. Router.routes['Homepage']                   # get the route by name
  196. Router.routes['PostShow'].path({_id: 1})    # return '/posts/1'
  197. Router.routes['PostShow'].path({_id: 1}, {  # return '/posts/1?sort_by=created_at#someAnchorTag'
  198.  query: 'sort_by=created_at',              # the query variable can also be a regular JavaScript object
  199.  hash: 'someAnchorTag'
  200. })
  201.  
  202. Router.go('Homepage')            # redirect to the defined route (here: '/')
  203. Router.go('PostShow', {_id: 7})  # redirect to '/posts/7'
  204.  
  205. Router.path('Homepage')          # return the path of the defined route as a string. (here: '/')
  206. Router.current().path            # return the current path
  207. //- By default, the router renders the current template directly into the body.
  208. //- If you'd like to share common HTML between routes, you can create your own layout:
  209. //- More: https://github.com/EventedMind/iron-router#layouts--rendering
  210. template(name="Layout")
  211.  
  212.  aside
  213.    {{> yield region='aside'}}
  214.  
  215.  .content
  216.    {{> yield}}
  217.  
  218.  footer
  219.    {{> yield region='footer'}}
  220.  
  221.  
  222. //- To use a route in your app, you can use the {{pathFor}} handlebars helper:
  223. //- More: https://github.com/EventedMind/iron-router#using-routes
  224. template(name="Header")
  225.  
  226.  //- set the url to '/'
  227.  a(href="{{pathFor 'Homepage'}}") Return to Homepage
  228.  
  229.  //- given a context of {_id: 1} this will render '/posts/1'
  230.  a(href="{{pathFor 'PostShow'}}") Post Show
  231.  
  232.  //- given a context of {_id: 1} this will render '/posts/1?sort_by=created_at#someAnchorTag'
  233.  a(href="{{pathFor 'PostShow' query='sort_by=created_at' hash='someAnchorTag'}}") Post Show
  234.  
  235.  
  236. Like this article? Join us on Twitter !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement