Slide 1

Slide 1 text

Routing in Ember.js Dan McClain

Slide 2

Slide 2 text

Routing? Wat? • With a web application, URLs are the main user interface • Ember.js uses URLs to manage states (where am I? What do I want to do? • Sharing a URL shares the current state (sort of) • Preserves the back button (unless you disable it) http://jsbin.com/ewumic/1/edit

Slide 3

Slide 3 text

Defining Routes

Slide 4

Slide 4 text

Router Types • By default, uses hash urls: #/articles/1 • You can use the History API (if the user’s browser supports it): /articles/1 • You can disable the browser’s URL entirely by using ‘none’ http://jsbin.com/ewumic/1/edit

Slide 5

Slide 5 text

this.route(‘about’) Route AboutRoute Controller AboutController Template about Generated Objects

Slide 6

Slide 6 text

this.route() • Used for singular pages • Can take a path http://jsbin.com/amicax/1/edit

Slide 7

Slide 7 text

this.resource(‘articles’) Route ArticlesRoute Controller ArticlesController Template articles

Slide 8

Slide 8 text

this.resource(‘articles’,function(){ this.route(‘new’) }); • Passing function to resource creates an articles.index route • Routes defined by this call • articles/index • articles/new • Renders the articles template first, with the nested route within its outlet

Slide 9

Slide 9 text

this.resource(‘articles’,function(){ this.route(‘new’) }); Route ArticlesIndexRoute Controller ArticlesIndexController Template articles/index /articles/index

Slide 10

Slide 10 text

this.resource(‘articles’,function(){ this.route(‘new’) }); Route ArticlesNewRoute Controller ArticlesNewController Template articles/new /articles/new http://jsbin.com/ovirep/12/edit

Slide 11

Slide 11 text

this.resource() • Resources manage multiple routes • Can have dynamic segments • Can have nested routes and resources http://jsbin.com/ovirep/12/edit

Slide 12

Slide 12 text

Route Hooks • model • setupController • activate • deactivate • renderTemplate

Slide 13

Slide 13 text

Specifying Models • Ember will call the model function of the current route • Dynamic segments will be passed in on an object provided to the function http://jsbin.com/ovirep/13/edit

Slide 14

Slide 14 text

Setting up controllers • Called when the route changes • Set properties on the controller, fetch additional resources http://jsbin.com/ecocen/1/edit

Slide 15

Slide 15 text

Activate & Deactivate • Activate is called when you enter a route for the first time, not when the model changes • Deactivate is called when you completely exit a route

Slide 16

Slide 16 text

Rendering Templates • Router’s main job is to render the correct template • Defining a renderTemplate function to change the template used or to render it in a different outlet http://jsbin.com/itofom/3/edit

Slide 17

Slide 17 text

Questions? • More information - http://emberjs.com/guides/routing/ • http://danmcclain.net • http://github.com/danmcclain • @_danmcclain