0

I'm trying to do a custom url.

More specifically, what I've already done is: mypage.com/details/1

What I'd like to do is: mypage.com/details/john-doe

Of course, I want exactly the same behavior as now, with the sole difference being a custom string instead of id

Here's my DS model

App.Images.FIXTURES = [
{
            id:1,
            name: "John",
            lastName: "Doe",
            age: 25,
            customUrl: "john-doe"
},
{
            id:2,
            name: "John",
            lastName: "Doe",
            age: 31,
            customUrl: "john-doe1"
}];

*Note the customUrl attribute which is unique.

My routes:

App.Router.map(function(){
        this.resource("details", {path: 'details/:image_id'});
    });

And the according html:

{{#linkTo 'details' this}}The details{{/linkTo}}

What are my options? Putting "this" in html automatically grabs the ID. I tried this.customUrl with no luck (I get the "undefined" value in the url).

Thank you for your help!

None None
  • 537
  • 2
  • 8
  • 15

1 Answers1

0

you can override serialize in router to return customUrl instead of id

App.DetailsRoute = Ember.Route.extend({
    //   ....
    serialize: function(model) {
        return { details_id: model.get('customUrl') };
    }
});
BenMorel
  • 34,448
  • 50
  • 182
  • 322
wedens
  • 1,782
  • 14
  • 18
  • This actually does the job, but if I then reload the page, it doesn't get displayed. When I click on image and get re-routed to details, it's still the ID that gets search by (`return App.Images.find(params.image_id);`, the json is: `{"image_id":"1"}`) even though I have implemented that serialize function. So when the user reloads the page, the json is different: `{"image_id":"john-doe"}`. How could I fix this, too? Because now the url is alright and the page is displayed, but if I give someone just the url of my page, nothing is displayed. – None None Aug 20 '13 at 14:06
  • try to use `query` method instead of `find`. `App.Images.query({'image_id':'john-doe'})` – wedens Aug 20 '13 at 14:12