jeudi 29 janvier 2015

ember need to refresh browser to get the model data

I follow the ember guide and with a little modification to show the post data. I defined a posts route which will generate links and a post route with dynamic segment to show the detail. however, if I click the link '/posts/1', it navigates to the post route with id. however, I do not see the post detail unless I refresh the browser. does anyone knows why? and can anyone explain what the route serialize hook does? I do not understand the explanation from the ember guide.


Handlebars



<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>

{{outlet}}
</script>

<script type="text/x-handlebars" id="posts">
<ul>
{{#each post in model}}
<li>{{#link-to 'post' post}}{{post.name}}{{/link-to}}</li>
{{/each}}
</ul>
</script>

<script type="text/x-handlebars" id="post">
<ul>
{{#each post in model}}
<h1>{{ post.name }} - {{ post.age }}</h1>
{{/each}}
</ul>
</script>


Ember Code



App = Ember.Application.create();

App.Router.map(function() {
this.resource('posts');
this.resource('post', { path: '/posts/:post_id' });
});

App.PostsRoute = Ember.Route.extend({
model: function () {
return [{
id: 1,
name: 'Andy',
age: 18
}, {
id: 2,
name: 'Tom',
age: 14
}, {
id: 3,
name: 'John',
age: 10
}];
}
});

App.PostRoute = Ember.Route.extend({
model: function (params) {
var obj = [{
id: 1,
name: 'Andy',
age: 18
}, {
id: 2,
name: 'Tom',
age: 14
}, {
id: 3,
name: 'John',
age: 10
}];
return obj.filter(function (item) {
return item.id === parseInt(params.post_id);
});
},
serialize: function(model) {
// this will make the URL `/posts/12` WTH is this mean????
return { post_id: model.id };
}
});




Aucun commentaire:

Enregistrer un commentaire