mercredi 3 juin 2015

Model data doesn't show up in view when directly accessing deep-link

I'm trying to build an ember application with SEO-friendly URLs.

For now, I have a single Model, which is called chapter. There's an index route that shows all chapters and a show route, which displays an individual chapter.

Here's the router:

Router.map(function() {
  this.resource('chapters', { path: '/' }, function(){
    this.route('show', { path: ':path' });
  });
});

So the URL / will show me an overview of chapters, and /chapter-1 will show the detail of the chapter that has the path property set to chapter-1.

This works fine, when I start at /, but when I try to access /chapter-1 directly, I don't get any model data in my view.

My index route accesses the data like so:

model: function(){
  return this.store.find('chapter'); 
}
/* Sends API request to /api/chapters, response:
{
  "chapters": [
    {
      "id": 1,
      "title": "Chapter 1",
      "path": "chapter-1"
    },
    {
      "id": 2,
      "title": "Chapter 2",
      "path": "chapter-2"
    }
  ]
}
*/

While the show route uses:

model: function(params){
  return this.store.find('chapter', params);
}
/* URL /chapter-1 Sends API request to /api/chapters?path=chapter-1
 * Response:
{
  "chapter": {
    "id": 1,
    "title": "Chapter 1",
    "path": "chapter-1"
  }
}

From my point of view, everything seems to be wired correctly. I'm getting all chapters in the index route and the desired chapter in the detail-route.

Here's the template for show:

<h2>{{model.title}}</h2>

When I enter / and then visit /chapter-1 by clicking a {{#link-to}} link, the title gets populated correctly. When I visit /chapter-1 directly, no title shows up. Can somebody point out what I'm doing wrong?




Aucun commentaire:

Enregistrer un commentaire