Search code examples
ember.jsember-dataember-cli

Redirecting to the first item in an Ember array, alphabetically?


I've got an array of pages that I'm sorting by title like so:

PagesController = Ember.ArrayController.extend
  sortProperties: ['title']

I'm also trying to redirect to the first item in the array when the user hits the pages.index route.

PagesIndexRoute = Ember.Route.extend
  redirect: (model) ->
    page = model.get 'firstObject'
    @transitionTo 'page', page if page?

Problem is, the firstObject is not the first item in the sorted array, so the user is plopped in somewhere in the middle of the list. In the template, I can loop through the arrangedContent property in order to get the sorted array. However, in the route, I have no access to that property or the sorted array.

How can I redirect to the first object in the array, after the array has been sorted alphabetically?


Solution

  • You should be able to access PagesController in PagesIndexRoute's redirect action via method @controllerFor('pages'). Then you can access its sorted content. Please try following code:

    PagesIndexRoute = Ember.Route.extend
      redirect: ->
        page = @controllerFor('pages').get 'firstObject'
        @transitionTo 'page', page if page?