【发布时间】:2015-01-29 16:27:19
【问题描述】:
我遵循 ember 指南并稍作修改以显示发布数据。我定义了一个将生成链接的帖子路由和一个带有动态段的帖子路由以显示详细信息。但是,如果我单击链接“/posts/1”,它会导航到带有 id 的发布路线。但是,除非我刷新浏览器,否则我看不到帖子的详细信息。有谁知道为什么?任何人都可以解释路由序列化钩子的作用吗?我不明白 ember 指南的解释。
车把
<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>
灰烬代码
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 };
}
});
【问题讨论】:
-
现在我知道序列化是做什么的了,谁能解释一下为什么我需要刷新浏览器来获取数据而不是第一次点击生成的链接?
标签: javascript ember.js handlebars.js ember-router