A question from an Ember noob…
I’m trying to load content onto my Ember app from a local JSON file, using a MAMP setup as a dev environment. Content is loading fine on a top-level route but I am having trouble getting things to work in a nested route.
my JSON file looks like this: //books.json { “books”: [ { “id”: 1, “title”: “1984”, “author_name”: “George Orwell” }, { “id”: 2, “title”: “The Shining”, “author_name”: “Stephen King” } ] }
Then I have an app.js
file in the same directory as books.json
that looks like this:
// lots of code like Ember.Application.create() removed for brevity…
// The router that defines both the top-level and nested route looks like this:
App.Router.map(function() {
this.resource(“books”, function(){
this.resource(“book”, { path: “:book_id”});
});
});
// returns the entire list of books
App.BooksRoute = Ember.Route.extend({
model: function() {
return $.getJSON("/js/books.json").then(function(data){
return data.books.map(function(book) {
book.body = book.title;
return book;
})
});
}
});
// returns a single book App.BookRoute = Ember.Route.extend({ model: function(params) { return $.getJSON(“/js/books.json?id=”+params.book_id).then(function(data){ data.book.body = data.book.title; return data.book; }); } });
The template where a single book gets passed to looks like this:
<script type="text/x-handlebars" id="book">
<article>
<p>{{body}}</p>
</article>
</script>
The problem is, when I load nested route into a browser and then refresh it, I get the following error:
Uncaught TypeError: Cannot read property 'title' of undefined
I’ve been using the Ember screencast tutorial here as a guide and also reviewed the JSON API spec looking for an answer but haven’t found one. Any ideas?
Thanks much!!!