我想在 Ember.js 中获取友好的 URL,但目前它仅适用于 ID。
如何使用 /#/post/sample-title ?
也许这会更容易(并且对我有效):/#/12/sample-title(12 是 ID)。
路由器.js
this.resource('post', { path: '/post/:post_slug/' });
post_route.js
(...)
return App.Post.find(model.post_slug);
感谢您的任何帮助!
我想在 Ember.js 中获取友好的 URL,但目前它仅适用于 ID。
如何使用 /#/post/sample-title ?
也许这会更容易(并且对我有效):/#/12/sample-title(12 是 ID)。
路由器.js
this.resource('post', { path: '/post/:post_slug/' });
post_route.js
(...)
return App.Post.find(model.post_slug);
感谢您的任何帮助!
要获取模型的不同属性以用作 URL 中的动态段,您需要使用serialize路由的钩子:
App.PostRoute = Ember.Route.extend({
serialize: function(model) {
return {
post_slug: model.get('post_slug')
};
}
});
希望能帮助到你。
对于余烬 3
export default class PostRoute extends Route {
serialize(section) {
return { post_slug: section.get('post_slug') };
}
}