Aaron Renoir
Aaron Renoir

Reputation: 4381

ember routing serialize and deserialize params

I have a ember route that uses a date parameter to retrieve a collection of events. I am using the deserialize function to fetch the collection. After the collection is fetched the url changes from "/day/2012-10-08" to "/day/undefined".

The events include a range of dates so I can't use the data to serialize the date parameter. Instead I should be able to return the params on the url. My example below sets an attribute on the router "current_params" on deserialize and returns it on serialize. I am wondering if there is access to the params in the serialize method.

      day: Ember.Route.extend
        route: "/day/:date"

        deserialize: (router, params) ->
          router.set('current_params', params)
          router.get('store').findQuery( App.Event, { date: params.date } )


        serialize: (router, context) ->
          router.get('current_params')

        connectOutlets: (router, context) ->
          router.get('applicationController').connectOutlet( 'timecard', context )

Upvotes: 1

Views: 2135

Answers (1)

wmarbut
wmarbut

Reputation: 4685

I've seen the named routes get changed to undefined when the serialize method was failing, and though there isn't enough information here to be sure, I would strongly suspect that to be the case.

I think it is a mistake to depend on the router to keep a piece of information like that for you. It is not designed to be a repository for arbitrary info, just to keep your application state.

My suggestion would be to move that date range information into a controller. So if the controller is TimecardController then

TimecardController = Ember.ObjectController.extend
    #other controller stuff
    #more controller stuff

    event_date: new Date

Then in your router

deserialize: (router, param) ->
    @get('timecardController').set 'event_date', params.date
    @get('store').findQuery App.Event, {date: params.date}
serialize: (router, context) ->
    date = @get('timecardController.event_date')
    #or on older ember builds @getPath('timecardController.event_date')
    '' + date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate()

Upvotes: 1

Related Questions