Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js routing

I am struggling to find any good Ember.js routing examples.

Should I use an addon like this or I personally like the look of this?

I see there is a routes collection as part of the State object but I cannot find any examples of how to use it.

like image 277
dagda1 Avatar asked Apr 14 '12 05:04

dagda1


4 Answers

Routing has been recently made available in core Ember.js, see Tom Dale's blog post.

Core developer Yehuda Katz wrote a gist about the usage of the new routing feature. It's a good read, and besides routing also states how it can be integrated with controllers.

To get the basic idea, here's a code example taken from the gist:

App.Router = Ember.Router.extend({
  root: Ember.State.extend({
    index: Ember.State.extend({
      route: '/',
      redirectsTo: 'calendar.index'
    }),

    calendar: Ember.State.extend({
      route: '/calendar',

      index: Ember.State.extend({
        route: '/'
      }),

      preferences: Ember.State.extend({
        route: '/preferences'
      })
    }),

    mail: Ember.State.extend({
      route: '/mail',

      index: Ember.State.extend({
        route: '/'
      }),

      preferences: Ember.State.extend({
        route: '/preferences'
      })
    })
  })
});

// If the user navigates to the page with the URL
// www.myapp.com/, you will start in the root.calendar.index state.
// The redirection to the calendar.index state will cause the URL
// to be updated to www.myapp.com/calendar

router.transitionTo('preferences');

// URL => www.myapp.com/calendar/preferences

router.transitionTo('mail.preferences');

// URL => www.myapp.com/mail/preferences

router.transitionTo('index');

// URL => www.myapp.com/mail
like image 71
pangratz Avatar answered Sep 24 '22 20:09

pangratz


I use sproutcore-routing in my apps because:

  • it plays nice with ember.js
  • it is actively maintained
  • it is used by some bigger ember.js apps (e.g. have a look at travis-ci routing code)

For documentation have a look at the spoutcore-routing tests

like image 34
fpauser Avatar answered Sep 23 '22 20:09

fpauser


I strongly recommend this guide on the ember site: http://emberjs.com/guides/router_primer/

I believe this content is very new-- I certainly didn't notice it a couple of weeks ago when I first started trying to understand Ember routing.

like image 37
Boon Avatar answered Sep 21 '22 20:09

Boon


Ember routing changed entirely over Christmas. They posted some new documentation to help, but removed all the old tutorials. I'm guessing the old ways of doing things (in the previous answers) will be deprecated. http://emberjs.com/guides/routing/

like image 26
Gopherkhan Avatar answered Sep 23 '22 20:09

Gopherkhan