Serialize ember-data models into query-params


#1

I want to bind a controller property that contains an ember-data object to a query string param. I’m using 1.6.0-beta.1+canary with the following in my controller.

export
default Ember.Controller.extend({
    queryParams: ['discipline'],
    discipline: null
});

I can’t find a way to serialize/deserialize the ember-data object (project) to/from the url though. Is it possible yet?


#2

I see a good suggestion at https://gist.github.com/machty/7923797 but it doesn’t look like it’s been implemented. In this case it would look like:

export
default Ember.Controller.extend({
    queryParams: {
        discipline: {
            serialize: function(discipline){
                return discipline.get("id");
            },
            deserialize: function(disciplineId){
                return this.store.get(disciplineId);
            }
        }
    },
    discipline: null
});

#3

I’m pretty sure @machty said Object query-params will be added “over his dead body” or maybe something about him spinning in his grave.

Serializing POJO in the url via query-params would get pretty out of hand. Someone in #emberjs asked about doing it a while back and agreed the results were nasty.

That said getting an ember-data record from a bound query-param should be pretty easy using a Computed Property.

App.MyController = Ember.ObjectController.extend({
  queryParams: ['recordId']
  recordId: null,
  record: function(key, value) {
    if (arguments.length > 1) {
     //handle set by setting recordId
     this.set('recordId', value.get('id'); 
   }
   return this.store.find('record', this.get('recordId'));
  }.property('recordId')
});

Might be errors, but basic idea should work. Hope this helps.

-ray


#4

Thanks for the suggestion about using a computed property, I wasn’t aware that you could handle setting the value as well. Going a step further I’ll see if I can create a mixin that would do it for any specified query params.