# Improving the Route#serialize behavior

**URL:** <https://discuss.emberjs.com/t/improving-the-route-serialize-behavior/1956>\
**Category:** Uncategorized\
**Created:** [July 29, 2013, 1:57am UTC](https://discuss.emberjs.com/t/improving-the-route-serialize-behavior/1956 "2013-07-29T01:57:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![southpolesteve](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/southpolesteve/32/14690_2.png) [@southpolesteve](https://discuss.emberjs.com/u/southpolesteve)\
**Post date:** [July 29, 2013, 1:57am UTC](https://discuss.emberjs.com/t/improving-the-route-serialize-behavior/1956/1 "2013-07-29T01:57:08Z")

</div>

I’m reposting this here from a PR ([https://github.com/emberjs/ember.js/pull/2933](https://github.com/emberjs/ember.js/pull/2933))

It seems to me that we could make the default serialize hook smarter about how it gets information from the model. Consider the following (slightly simplified) code extracted from my own app that browses a list of Github repos:

```javascript
Github.Router.map(function() {
  this.resource('repo', { path: '/:owner/:name' });
});

Github.RepoRoute = Ember.Route.extend({
  model: function(params) {
    return Github.Repo.find(params);
  },
  serialize: function(model, params) {
    return { owner: model.get('owner'), name: model.get('name') };
  }
});

```

Template:

```auto
{{#linkTo 'repo' repo}}{{repo.full_name}}{{/linkTo}}

```

The serialize hook could be intelligent in cases where the data and URLs are not structured around IDs but other properties on the model. If I define my path as `/:owner/:name` it would try and construct the URL using `model.get('owner')` and `model.get('name')`.

Thoughts?

---

<div class="post-metadata">

**Author:** ![kingpin2k](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kingpin2k/32/4255_2.png) [@kingpin2k](https://discuss.emberjs.com/u/kingpin2k)\
**Post date:** [July 29, 2013, 5:25am UTC](https://discuss.emberjs.com/t/improving-the-route-serialize-behavior/1956/2 "2013-07-29T05:25:19Z")

</div>

I agree, that seems like a good default behavior for the serializer. We’re in a similar situation, except we have a composite key.

Then people whose models don’t match the slugs override the serialize hook still (probably without the params that you have in there, I think that’s just a typo though) to provide the correct behavior.

At the same time it’d be nice if the find methods supported more than just id etc, I’m always having to override it as well, but I guess that’s a different issue.

---

<div class="post-metadata">

**Author:** ![drogus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/drogus/32/14200_2.png) [@drogus](https://discuss.emberjs.com/u/drogus)\
**Post date:** [July 29, 2013, 11:18am UTC](https://discuss.emberjs.com/t/improving-the-route-serialize-behavior/1956/3 "2013-07-29T11:18:36Z")

</div>

👍

`{{linkTo}}` behavior is already a bit better thanks to @machty, who joined his and mine work [to make `linkTo` arguments bound](https://github.com/machty/ember.js/commit/aec74d399a65dfadb43ab73365cdfdced2d4be73). This is a great improvement, but attribute used in serialize is still not bound.

With your patch and with changes from [#2252](https://github.com/emberjs/ember.js/pull/2252) we could make the situation even better. In such case, people would have to do something **only** in case where they use something different than id **and** they use properties different than specified in the router.

The only problem I can see here is that usually you use camelized names for properties, while you use underscored versions for URL attributes. If someone does it differently it may be hard to track and confusing.

---

<div class="post-metadata">

**Author:** ![southpolesteve](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/southpolesteve/32/14690_2.png) [@southpolesteve](https://discuss.emberjs.com/u/southpolesteve)\
**Post date:** [August 13, 2013, 2:26am UTC](https://discuss.emberjs.com/t/improving-the-route-serialize-behavior/1956/4 "2013-08-13T02:26:53Z")

</div>

I finally had a chance to write up a proper PR: [https://github.com/emberjs/ember.js/pull/3134](https://github.com/emberjs/ember.js/pull/3134)

I can also the camelized vs underscore properties issue. I’m happy to add a conversion to camelcase to the PR if everyone thinks it makes sense… It is easy enough to change.
